# REGEX help

**URL:** <https://community.openhab.org/t/regex-help/149308>\
**Category:** Scripts & Rules\
**Tags:** regex\
**Created:** [September 3, 2023, 11:46am UTC](https://community.openhab.org/t/regex-help/149308 "2023-09-03T11:46:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rockit4](https://community.openhab.org/user_avatar/community.openhab.org/rockit4/32/43610_2.png) [@rockit4](https://community.openhab.org/u/rockit4)\
**Post date:** [September 3, 2023, 11:46am UTC](https://community.openhab.org/t/regex-help/149308/1 "2023-09-03T11:46:45Z")

</div>

Hi,

I’m running the MQTT binding in OH4 and I am trying to make use of the REGEX binding as well 🙂  
I have following stated under the “Incoming Value Transformations” in the MQTT generic item:

```yaml
REGEX:(?<=1-0:31.7.0\(0)(.*)(?=\*A\)\\r\\n1-0:5)

```

But why is this not working when I am trying to transform an incoming MQTT string and capture 5.5 from the the paramter 1-0:31.7.0 in the string below?

```yaml
/ELL5\\253941227_A\r\n\r\n0-0:1.0.0(230903100128W)\r\n1-0:1.8.0(00002557.779*kWh)\r\n1-0:2.8.0(00000000.162*kWh)\r\n1-0:3.8.0(00000730.106*kvarh)\r\n1-0:4.8.0(00000005.070*kvarh)\r\n1-0:1.7.0(0000.116*kW)\r\n1-0:2.7.0(0000.000*kW)\r\n1-0:3.7.0(0000.069*kvar)\r\n1-0:4.7.0(0000.050*kvar)\r\n1-0:21.7.0(0000.010*kW)\r\n1-0:41.7.0(0000.007*kW)\r\n1-0:61.7.0(0000.098*kW)\r\n1-0:22.7.0(0000.000*kW)\r\n1-0:42.7.0(0000.000*kW)\r\n1-0:62.7.0(0000.000*kW)\r\n1-0:23.7.0(0000.000*kvar)\r\n1-0:43.7.0(0000.000*kvar)\r\n1-0:63.7.0(0000.069*kvar)\r\n1-0:24.7.0(0000.014*kvar)\r\n1-0:44.7.0(0000.035*kvar)\r\n1-0:64.7.0(0000.000*kvar)\r\n1-0:32.7.0(241.2*V)\r\n1-0:52.7.0(241.0*V)\r\n1-0:72.7.0(241.1*V)\r\n1-0:31.7.0(005.5*A)\r\n1-0:51.7.0(000.1*A)\r\n1-0:71.7.0(000.5*A)\r\n!41FB\r\n

```

Side note, I am not sure if the “\n” and “\r” are being processed by the REGEX transformation. If I look at the item in the UI there are no “\n” or “\r”, but if I check the item via the REST API it is shown as above. But I have tried my REGEX statement with and without the extra characters and still get the same result.

Example:

> **[RegExr: Learn, Build, & Test RegEx](https://regexr.com/7jhsk)**
>
> RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).

Help appreciated.

---

<div class="post-metadata">

**Author:** ![pacive](https://community.openhab.org/user_avatar/community.openhab.org/pacive/32/36234_2.png) [@pacive](https://community.openhab.org/u/pacive)\
**Post date:** [September 3, 2023, 12:55pm UTC](https://community.openhab.org/t/regex-help/149308/2 "2023-09-03T12:55:50Z")

</div>

`\r\n` means there’s a line break in the text, which is a bit tricky to handle in regex, since it sometimes only handles one line at a time. Not sure how the regex transformation in OH handles it though. But I would suggest to simplify the expression so you get some output working, and then add more to it if needed. Especially leave out the look-ahead/behind, they often complicate things more than they help.

Try something like this for example:  
`1-0:31\.7\.0\(0([0-9.]+)`

---

<div class="post-metadata">

**Author:** ![rockit4](https://community.openhab.org/user_avatar/community.openhab.org/rockit4/32/43610_2.png) [@rockit4](https://community.openhab.org/u/rockit4)\
**Post date:** [September 3, 2023, 1:57pm UTC](https://community.openhab.org/t/regex-help/149308/3 "2023-09-03T13:57:15Z")

</div>

> [@pacive](#):
>
> 1-0:31.7.0(0([0-9.]+)

Simpler is better 🙂  
I’ve tried to google a solution to removing the first part of the output I get with your REGEX?

The output I get is:

```yaml
1-0:31.7.0(005.5

```

and I need to remove:

```yaml
1-0:31.7.0(0

```

I only want the 05.5 value.

_swe_ Tack för hjälpen 🙂 _/swe_

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [September 3, 2023, 2:04pm UTC](https://community.openhab.org/t/regex-help/149308/4 "2023-09-03T14:04:26Z")

</div>

`.*1-0:31\.7\.0\(0([0-9.]+).*`

---

<div class="post-metadata">

**Author:** ![rockit4](https://community.openhab.org/user_avatar/community.openhab.org/rockit4/32/43610_2.png) [@rockit4](https://community.openhab.org/u/rockit4)\
**Post date:** [September 3, 2023, 2:28pm UTC](https://community.openhab.org/t/regex-help/149308/5 "2023-09-03T14:28:53Z")

</div>

> [@jimtng](#):
>
> ._1-0:31.7.0(0([0-9.]+)._

Thank you!
