REGEX - did something change?

Tags: #<Tag:0x00007f1867240de8>

I used successfully REGEX in some rules, but now they do not work anymore.
(OH 2.5M5)
Did it change recently?

The REGEX tester online help a little, but do not match the syntax of REGEX used in OH.
Is there any REGEX tester which behaves like the OH implementation?

I think the issue is mainly with special characters like " > and such.

The actual library that handles the REGEX expression is built into Java and since Java really hasn’t changed it is unlikely that there was a relevant change for this.

Could you provide some examples of what worked before and doesn’t work now along with the String your are trying to match against and what you are trying to extract from it.

On thing to remember about OH REGEX is you must match the whole String and it will return the first group (i.e. first ( )). Also, because of the way you need to define the expression in a String, there is likely some escaping you need to apply to the expression that is not required by the online testers.

One change I do think was made, but I’m not sure if it is limited to just the MQTT binding or not, where it will not return anything if the expression doesn’t match the String at all.

Thanks, Rich.
I figured it out. maybe my previous REGEX filter was too complex!?

This is the previous syntax:
val String locked = transform("REGEX", ".*>Maintenance mode</td><td class=\"table-cell\">(......).*", json)

This one is working now:
val String locked = transform("REGEX", ".*Maintenance mode.*cell'>(..).*enableLockedMode.*", json)

Part of the string to match against:

> <tr class='table-row'><td class='table-cell'>Kiosk mode</td><td class='table-cell'>on <a href='javascript:askAndUnlockKiosk();void(0);' class='button'>Unlock</a></td></tr>
> <tr class='table-row'><td class='table-cell'>Motion detection</td><td class='table-cell'>on <a href="javascript:toggleImage('?cmd=getCamshot');void(0);" class='button'>Cam shot</a></td></tr>
> <tr class='table-row'><td class='table-cell'>Acoustic detection</td><td class='table-cell'>off</td></tr>

Seeing this code I just recognize, that the code of the tablet response (FUlly Kiosk Browser) must have changed in the meantime.
>(......) vs (..)

sometimes it needs an external view upon things.
So, never mind and thanks a lot for pushing me into the right direction.

If it is JSON (your variable is named “json”), why don’t you use JSONPATH instead? It really looks like it’s XML (or HTML), so why don’t you just use “XPATH” instead?

That’s a good question.
I never thought about this.and the name json is just a copy and paste stuff from (real) json syntax processing.
I will look into this.
Thanks for the hint!

Is there an advantage?
I see that some people struggle with it here as well and with REGEX / JSON I am on a reasonable level of knowledge now.

If the string canes a little such as stuffy spreading is different order XPATH will be much better able to handle it. REGEX retires the String you bee exactly the same every time so it much more brittle.

XPATH and JSONPATH are also much easier to understand. For REGEX you basically need to have the string up next to the REGEX to understand what it does. With XPATH and JSONPATH you can just see the way the party navigates through the hierarchical structure.

In my opinion, REGEX should only be used as a last resort, not as a first choice.

Most of the ones you see with JSONPATH and xpath have to do with the fact that, much like with REGEX, the way oh uses them is slightly off the standard.

2 Likes

Thanks @rlkoshak,
That makes sense and I will start playing around with XPATH then.