MQTT Binding matching REGEX in transformationPattern

I don’t know if this is the right place but I have a problem to match something in the transformationPattern with regex.

I’m trying to get the status back from an McLighting Node.

I get something like “OK #4aa044” in the mqtt channel. All my attempts to match everything after the “#” were unsuccessfull.

The documentation for using regex at this point is very little.

Has anybody a idea how to do it?

I’m not very good at REGEX but here is a good tool that may help. https://regex101.com/

Won’t help… Thanks for the hint. From Forum posts I got that the REGEX in Openhab seems to work a little different. But could not solve my problem with that.

Went to JS and a split funktion for the solution. It’s a lot more code but it works

It works a little different in that your REGEX must match the entire String, and it returns the first matching group (i.e. stuff in parens). Normally, you just need to match the part that you want from the String in other uses of REGEX.

Beyond that, the syntax for REGEX is exactly the same. .*#(.*) should work.

Thanks a lot. How can I do match the String so that a String like “OK #FFF333” online after the # is matches an not on an string like “OK ##FFF333”?

I don’t answer the question. You want everything after one or more “#” characters?

.*#+(.*)

You want to ignore cases where there is more than one “#”? Assuming that the string after the # is only letters and numbers)

.*#(\w*)

Instead of using “.” which means any character we use \w which only matches letter, numbers and underscore. So that expression won’t match anything in “OK ##FFF333”.

REGEX is a whole topic unto itself with whole books written about it.

Sorry… I tried the whole day…

In the MQTT Topic can come three (there are more but three for the colors) variations of this String:
OK #FFFFFF (for the first Color)
OK ##FFFFFF (for the second Color)
OK ###FFFFFF (for the third Color)

So the String after the '#'s could consist of letters an numbers for RGB color. The complete documentation can be found here: https://github.com/FabLab-Luenen/McLighting/wiki/WebSocket-API-(V3.1)

With your first regex it matches when “OK ###F3F3F3” is pushed to the channel to all three implementations.

RegEx 1: .*#+(.*)
RegEx 2: .*##+(.*)
RegEx 3: .*###+(.*)

I’ve tried to specify with OK\\s#(.*) but this doesn’t seem to work.

Another response could be : OK ?254 (for the speed), but that is noch the point right now.

Of course, the “+” means “one or more”.

That won’t do anything.

To requote myself since you appear to have ignored it:

You want to ignore cases where there is more than one “#”? Assuming that the string after the # is only letters and numbers)

.*#(\w*)

Instead of using “.” which means any character we use \w which only matches letter, numbers and underscore. So that expression won’t match anything in “OK ##FFF333”.

To match only the first variation:

.*#(\w*)

To matching only the second variation:

.*##(\w*)

To matching only the third variation:

.* ###(\w*)

As I said above, “.” matches any character, including “#”. That’s why you need to use \w so the stuff inside the () doesn’t match any extra # that may exist.

  • . : match any character
  • *: do the previous match 0 or more times
  • #: match exactly the character “#”
  • +: match the previous character one or more times
  • \w: match only letters and numbers and underscores

So .*#(\w*) means match any number of any character up to the first # you see, then match and return all the letters and numbers that follow the “#”. If you have more than one “#” the expression won’t match because “#” doesn’t get matched against with “\w”.

As I said, regular expressions are a whole topic unto themselves. Fully documenting them is outside the scope of openHAB. But there are tons of tutorials and guides on the web.

Thank you. Yes indeed you are absolutely right, documentation all this would be to much.

For me it’s easyer to work from examples and the one mentioned in the binding doc is a little bit confusing.

I found a solution:
REGEX:OK\s#(\w{6})
REGEX:OK\s##(\w{6})
REGEX:OK\s###(\w{6})