Regex in incoming transformation

Hi there,

I need a little startup help with regex incoming transformations.

I have a MQTT-Thing which retrieves data from my MQTT-broker. I need to transform the incoming data.

The value from the MQTT-Thing looks like this 27.34;ok
I need just the 27.34 as a Number.

I thought

REGEX:(.....)

would do it on the channel config would do it (incoming value transformation). But it doesn’t work.

Any hints? I guess I’m just missing a little thing (newbee problem :wink: )

use the correct REGEX :slight_smile:

REGEX:(\d+\.\d*);.*

should do the trick.

1 Like

I want to explain why your REGEX didn’t work so you are better able to create them in the future.

There are a couple of odd behaviors in the way the REGEX transform works in openHAB. The biggest quirk is that the transform must match the entire string and it’s the first matching group (i.e. ( )) that gets returned. Your regex fails on the first point, it only matches Strings that consist of five characters so it skips the messages you are receiving which have 8 characters.

It is possible to adjust your REGEX to work. REGEX:(.....).*

@Udo_Hartmann’s solution is even better because it is more precise. It matches one or more digits, a decimal point, and zero or more digits. So if you got a message like “error” your REGEX would match but Udo’s would not.

But we can make Udo’s even better. Currently it won’t support a minus sign and the decimal point is required to be there.

REGEX:([-]?\d+[\.]?\d*);.*

That should match and return the negative sign if it’s present, one or more digits, optionally match a decimal point, and then zero or more digits, ignoring the “;” and everything after that.

But beware, I just typed this in and it’s not tested.

2 Likes

Thanks to all of you!