Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MAP] Minor corrections in README #16286

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions bundles/org.openhab.transform.regex/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ The special characters `\.[]{}()*+-?^$|` have to be escaped when they should be

### Basic Examples

| Input String | Regular Expression | Output String | Explanation |
|---------------------------|------------------------|----------------------------|--------------------------|
| `My network does not work.` | `s/work/cast/g` | `"My netcast does not cast."` | Replaces all matches of the string "work" with the string "cast". |
| `My network does not work.` | `.*(\snot).*` | `" not"` | Returns only the first match and strips of the rest, "\s" defines a whitespace. |
| `temp=44.0'C` | `temp=(.*?)'C)` | `44.0` | Matches whole string and returns the content of the captcha group `(.?)`. |
| `48312` | `s/(.{2})(.{3})/$1.$2/g` | `48.312` | Captures 2 and 3 character, returns first capture group adds a dot and the second capture group. This divides by 1000. |
| Input String | Regular Expression | Output String | Explanation |
| --------------------------- | ------------------------ | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| `My network does not work.` | `s/work/cast/g` | `"My netcast does not cast."` | Replaces all matches of the string "work" with the string "cast". |
| `My network does not work.` | `.*(\snot).*` | `" not"` | Returns only the first match and strips of the rest, "\s" defines a whitespace. |
| `temp=44.0'C` | `temp=(.*?)'C` | `44.0` | Matches whole string and returns the content of the captcha group `(.?)`. |
| `48312` | `s/(.{2})(.{3})/$1.$2/g` | `48.312` | Captures 2 and 3 character, returns first capture group adds a dot and the second capture group. This divides by 1000. |

### Example In Setup

Expand All @@ -40,22 +40,24 @@ the regex transformation can be used to extract the value to display it on the l
**.items**

```csv
String Temperature_str "Temperature [REGEX(.*=(\\d*.\\d*).*):%s °C]" {...}
String Temperature_str "Temperature [REGEX(.*=(\\d*\\.\\d*).*):%s °C]" {...}
Number Temperature "Temperature [%.1f °C]"
```

The regex pattern is is defined as follows
The regex pattern is is defined as follows:

* `.*` match any character, zero and unlimited times
* `=` match the equal sign literally, used to find the position
* `()` capture group match
* `\d*` match a digit (equal to [0-9]), zero and unlimited times, the backslash has to be escaped see [string vs plain](#Differences-to-plain-Regex)
* `.` match the dot literally
* `\w*` match a word character (equal to [a-zA-Z_0-9]), zero and unlimited times, the backslash has to be escaped see [string vs plain](#Differences-to-plain-Regex)
* `()` capture group match
* `\d*` match a digit (equal to [0-9]), zero and unlimited times
* `\.` match the dot literally
* `\w*` match a word character (equal to [a-zA-Z_0-9]), zero and unlimited times
* `.*` match any character, zero and unlimited times

The result will be `44.0` and displayed on the label as `Temperature 44.0°C`.
A better solution would be to use the regex on the result from the binding either in a rule or when the binding allows it on the output channel.
Note, the backslashes have to be escaped. See [string vs plain](#differences-to-plain-regex)

The result will be `44.0` and displayed on the label as `Temperature 44.0 °C`.
A better solution would be to use the regex on the result from the binding either in a rule or when the binding allows it on the output channel.
Thus the value `44.0` would be saved as a number.

**.rules**
Expand All @@ -66,7 +68,7 @@ rule "Convert String to Item Number"
Item Temperature_str changed
then
// use the transformation service to retrieve the value
val newValue = transform("REGEX", ".*=(\\d*.\\d*).*", Temperature_str.state.toString)
val newValue = transform("REGEX", ".*=(\\d*\\.\\d*).*", Temperature_str.state.toString)

// post the new value to the Number Item
Temperature.postUpdate( newValue )
Expand Down Expand Up @@ -102,7 +104,7 @@ Please note: This profile is a one-way transformation, i.e. only values from a d

* A full [introduction](https://www.w3schools.com/jsref/jsref_obj_regexp.asp) for regular expression is available at W3School.
* Online validator help to check the syntax of a regex and give information how to design it.
* [Regex 101](https://regex101.com/)
* [Regex R](https://regexr.com/)
* [ExtendsClass](https://extendsclass.com/regex-tester.html)
* [Softwium](https://softwium.com/regex-explainer/)
* [Regex 101](https://regex101.com/)
* [Regex R](https://regexr.com/)
* [ExtendsClass](https://extendsclass.com/regex-tester.html)
* [Softwium](https://softwium.com/regex-explainer/)
jlaur marked this conversation as resolved.
Show resolved Hide resolved