# Parse String. Regex?

**URL:** <https://discourse.nodered.org/t/parse-string-regex/21941>\
**Category:** General\
**Created:** [18 February 2020 18:21 UTC](https://discourse.nodered.org/t/parse-string-regex/21941 "2020-02-18T18:21:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![niclas.w82](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niclas.w82/32/31813_2.png) [@niclas.w82](https://discourse.nodered.org/u/niclas.w82)\
**Post date:** [18 February 2020 18:21 UTC](https://discourse.nodered.org/t/parse-string-regex/21941/1 "2020-02-18T18:21:40Z")

</div>

Hi.

I have a sting output that I want to split somehow. Have tried a few regex expressions but i have early used regex before so no luck. Any idea how to do this the best way?

the string lock like this ALARM,A:341N572964E012045499 A12002172140U AlarmButton R:5 B:4 G:F;

I want this to be splitted so it looks something like this

ALARM  
A:341  
N532464  
E014075491  
A12002172140U  
AlarmButton R:5 B:4 G:F;

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [18 February 2020 18:37 UTC](https://discourse.nodered.org/t/parse-string-regex/21941/2 "2020-02-18T18:37:16Z")

</div>

> [@niclas.w82](#):
>
> ALARM,A:341N572964E012045499 A12002172140U AlarmButton R:5 B:4 G:F;

If you use JSONata, you can use the following expression and you will get back an array containing the results:

```auto
$match(payload, /(.*),(.*)(N.*)(E.*)(A.*) (A.*)/).groups

```

---

<div class="post-metadata">

**Author:** ![niclas.w82](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niclas.w82/32/31813_2.png) [@niclas.w82](https://discourse.nodered.org/u/niclas.w82)\
**Post date:** [18 February 2020 19:38 UTC](https://discourse.nodered.org/t/parse-string-regex/21941/3 "2020-02-18T19:38:12Z")

</div>

Wow!! Thanks!!

---

<div class="post-metadata">

**Author:** ![niclas.w82](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niclas.w82/32/31813_2.png) [@niclas.w82](https://discourse.nodered.org/u/niclas.w82)\
**Post date:** [18 February 2020 21:46 UTC](https://discourse.nodered.org/t/parse-string-regex/21941/4 "2020-02-18T21:46:13Z")

</div>

One more question. $match(payload, /(._),(._)(N._)(E._)(A._) (A._)/).groups works fine. But if I want to remove N and E in the result can I do something like $match(payload, /(._),(._)(N._-1)(E._-1)(A._) (A._)/).groups have tried o lot of combinations but can not get the formatting right. If it works to do something like that?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [19 February 2020 00:18 UTC](https://discourse.nodered.org/t/parse-string-regex/21941/5 "2020-02-19T00:18:40Z")

</div>

> [@TotallyInformation](#):
>
> $match(payload, /(._),(._)(N._)(E._)(A._) (A._)/).groups

Not tried but probably something like:

```auto
$match(payload, /(.*),(.*)N(.*)E(.*)(A.*) (A.*)/).groups

```

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [19 February 2020 07:41 UTC](https://discourse.nodered.org/t/parse-string-regex/21941/6 "2020-02-19T07:41:19Z")

</div>

When working with regex, the regex101 website is a powerful tool to see if you’ve configured it correctly. It supports multiple languages’ flavour of regex, so make sure to set it to JavaScript first. On top of that it has a reference included for all functionality.  
Here’s Julian’s last regex, and I think it would work well: [https://regex101.com/r/TjwEID/1/](https://regex101.com/r/TjwEID/1/)

It matches as:

| Group 1. | n/a | ALARM |
| --- | --- | --- |
| Group 2. | n/a | A:341 |
| Group 3. | n/a | 572964 |
| Group 4. | n/a | 012045499 |
| Group 5. | n/a | A12002172140U |
| Group 6. | n/a | AlarmButton R:5 B:4 G:F |

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [19 February 2020 21:35 UTC](https://discourse.nodered.org/t/parse-string-regex/21941/7 "2020-02-19T21:35:29Z")

</div>

Yes, I regularly use various online regex tools for more complex requirements. This one was fairly easy and didn't need anything clever 😀

---

<div class="post-metadata">

**Author:** ![niclas.w82](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niclas.w82/32/31813_2.png) [@niclas.w82](https://discourse.nodered.org/u/niclas.w82)\
**Post date:** [20 February 2020 16:05 UTC](https://discourse.nodered.org/t/parse-string-regex/21941/8 "2020-02-20T16:05:42Z")

</div>

Thanks!! You guys are awesome 👍

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 April 2020 16:05 UTC](https://discourse.nodered.org/t/parse-string-regex/21941/9 "2020-04-20T16:05:43Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
