Going crazy about REGEX

Hello,
I try to read XML base values from a device which look this way:

<?xml version="1.0" encoding="UTF-8"?><temperature> 1.9</temperature>

The problem: There’s a leading space before the figure 1.9 - and the REGEX transformation in OH2 cannot handle this and crashes with a null point exception if I use the standard regex format for all characters (.*?)

I tried it with various variants like this, which should work according to various REGEX-helpers:

.*?<temperature>([0-9+.-]+)</temperature>.*?

Still, I get no valid value into my item. It’s either “NULL” or “-”
This is my item definition:

Number PoolTemp               "Pool-Temperatur [%.1f °C]"                 <pool>     {http="<[http://192.168.1.75:80?xml=1:60000:REGEX(.*?<temperature>([0-9.]+)</temperature>.*?)]"}

(it doesn’t work if I use string also…)

Does anybody have a hint for me?

Thank you,
Boby

Try
.*?<temperature>\\s*(\\d+\\.\\d+)</temperature>.*?
(not tested - just wrote it down)

Sebastian, you’re my hero! It works! Thank you so much!

I tried hundreds of cominbations and tested them on regex101, but now I learned that I have to use double backslashes in openHAB whereas the regex testers I know count double backlashes as error - or interpret them wrong.

Thank you once more!!!

Best regards,
Robert

Also you did forget the leading space in your regex. :wink:
Glad I could help. :slight_smile:

Hi Sebastian,

as you are such a regex Ninja wondering if you may be able to help with a similar issue I am having. I an receiving the following udp packet from my solar panel monitor

<solar id='4437191016C3'><timestamp>1507151560</timestamp><current><generating units='w'>0.00</generating><exporting units='w'>0.00</exporting></current><day><generated units='wh'>4248.00</generated><exported units='wh'>636.86</exported></day></solar>

I want to extract the exporting figure but everything I try just returns the full string. have tried both REGEX and XPATH but both to no avail!

any help you can give would be greatly appreciated

Try

.*?&lt;exported units='\\s+'&gt;(\\d+\\.?\\d*&lt;/exported&gt;.*?

Of course untested - just wrote it down

Hi thanks, sorry haven’t managed to try it out for a while then the batteries went flat in the sender but managed to replace them tonight. It did have an affect but sadly still didn’t deliver the value. I have it set up as follows

String TestSensor "TestState [%s]" <settings> { udp="<[192.168.1.4:*:REGEX(.*?&lt;exported units='\\s+'&gt;(\\d+\\.?\\d*&lt;/exported&gt;.*?))]"}

assuming the syntax is correct I get ‘null’ which is a change from the full string. Also crashes the ios app and I get an internal server error which is fixed when I remove the string from my items file.

event logs just show event change, nothing in openhab.log

2017-10-20 18:04:53.636 [ItemStateChangedEvent     ] - TestSensor changed from <solar id='4437191016C3'><timestamp>1508519093</timestamp><current><generating units='w'>241.00</generating><exporting units='w'>0.00</exporting></current><day><generated units='wh'>8623.75</generated><exported units='wh'>2948.97</exported></day></solar>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   to null

Sorry, type after units. Try this:

.*?&lt;exported units='\\w+'&gt;(\\d+\\.?\\d*&lt;/exported&gt;.*?

wow, that was quick, thanks so much for you help on this. Sadly still returning the same null value

code

String TestSensor "TestState [%s]" <settings> { udp="<[192.168.1.4:*:REGEX(.*?&lt;exported units='\\w+'&gt;(\\d+\\.?\\d*&lt;/exported&gt;.*?))]"}

log

2017-10-20 18:24:53.597 [ItemStateChangedEvent     ] - TestSensor changed from <solar id='4437191016C3'><timestamp>1508520293</timestamp><current><generating units='w'>225.00</generating><exporting units='w'>0.00</exporting></current><day><generated units='wh'>8700.64</generated><exported units='wh'>2948.97</exported></day></solar>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   to null

strange null values between the from and two values on the logs. not sure if that’s relevant image

Another typo:
it has to be exporting not exported

.*?&lt;exporting units='\\w+'&gt;(\\d+\\.?\\d*&lt;/exporting&gt;.*?

Wouldn’t it be better to use XPATH transformation rather than REGEX transformation for XML data?

Thanks again for your help on this. Not been able to play with this for a while but still no joy sadly, still displays null in the value and crashes the iPhone app quite spectacularly with a 400 error. :frowning:

Might have a look at xpath and see if I can do anything with that.

Hm, I am not sure why I was escaping the <.
Try this:

.*?<exporting units='w'>>\\s*(\\d+\\.\\d+)</exporting>.*?