Problem with getting substring via Regex in OH3

Hello,
I have problems with OH3 and Regex Patterns in the .items file. Following the RegEx tutorial: RegEx - Transformation Services | openHAB

.things file:
Thing http:url:daenetip3 “DAEnetIP3” [
baseURL=“http://some-url”,
timeout=5000,
refresh=7] {
Channels:

		Type string : doName1 []	

}

doName1 is something like: AS0=1;

My goal is to extract the value, which is 1 in this case. I tried many things in the .items file but without success:

  1. Tried to get it as a substring:
    String DO1Name “[REGEX(=(.*?);):%s]” { channel=“http:url:daenetip3:doName1” }
    Result is: [NULL]
    The interesting thing is as per the docs, this should work.

  2. Tried to replace strings and leave only the “1” but with partial success:
    String DO1Name “[REGEX(s/AS0=/ /g):%s]” { channel=“http:url:daenetip3:doName1” }
    Result is: [ 1;]

I didn’t find a way to replace with “” (delete it) but only with at least one char
Also I didn’t find a way to replace at the same time the “;” string so i can have only the “1” result.

Any help will be appreciated,
Thanks

the docs have .* before =

It is really difficult to see what’s going on here. Please use code fences for all code and logs posted to the forum.

```
code goes here
```

So you are trying to transform the state of the DO1Name Item using the REGEX transformation. Usually when extracting values like that the REGEX goes in the Thing configuration so you can use a Number Item.

An Item’s label usually has two parts: "<label> [<state>]". <label> is the static text that appears before the state as part of the label. The state shows the current state of the Item.

Often you’ll just see a String formatting field for the state (e.g. %d for integers, %.1f for a floating point value rounded to the nearest tenth, %s for a String).

Occasionally you will want to show the user the state transformed in some way. So far so good.

However, in OH one thing is a little different from regular use of REGEX. If you are using a capture group, only the first capture group is returned and the REGEX must match the entire String. The first capture group is what gets returned by the transformation.

So if you want the “1” from “AS0=1;” the expression needs to be something like .*AS0=(.*);.*. I’m assuming that the full value of the String has multiple lines or other entries and we need to match those too both the text before “AS0=” and the text after “;”.

Thank you!
I solved my problem as @rlkoshak suggested to implement the regex within the things.

Type switch : do1 [stateTransformation="REGEX:.*AS0=(.*?);AS1.*", onValue="1", offValue="0", commandExtension="AS0=%2$s&"]

now it works and we can offer to our customers OH3 examples for our DAEnetIP3 controller.
Thank you guys!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.