# Split msg.payload "P-131" to Letters and Numbers

**URL:** <https://discourse.nodered.org/t/split-msg-payload-p-131-to-letters-and-numbers/55429>\
**Category:** General\
**Created:** [19 December 2021 13:05 UTC](https://discourse.nodered.org/t/split-msg-payload-p-131-to-letters-and-numbers/55429 "2021-12-19T13:05:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![olby2](https://avatars.discourse-cdn.com/v4/letter/o/85f322/32.png) [@olby2](https://discourse.nodered.org/u/olby2)\
**Post date:** [19 December 2021 13:05 UTC](https://discourse.nodered.org/t/split-msg-payload-p-131-to-letters-and-numbers/55429/1 "2021-12-19T13:05:18Z")

</div>

Hello Forum,  
i want to process Data from a external Device and the Data looks like "P-131\_SPO=-200W"

i want to process the Filed "P-131\_SPO" -\> -131  
and The Value "-200W" -\> -200 (W)

split at "=" and "\_" works well, but i can´t find a splitter / seperator to the letters form the Numbers ( in String)

thanks for your help in advanced  
olby2

My Flow:  
( I split it here fixed with "P" but that is not a allover solution )

```auto
[
    {
        "id": "1ab3bd40d14573ed",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "df6d6c66.b6c4e",
        "type": "split",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "splt": "=",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "xy",
        "x": 550,
        "y": 260,
        "wires": [
            [
                "3ba2aff1.e8af5",
                "9052efa8.e5f0a"
            ]
        ]
    },
    {
        "id": "d9822859.20a7b8",
        "type": "inject",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": "",
        "topic": "",
        "payload": "P-131_SPO=-200W",
        "payloadType": "str",
        "x": 320,
        "y": 260,
        "wires": [
            [
                "bc23045.f5103f8",
                "df6d6c66.b6c4e"
            ]
        ]
    },
    {
        "id": "2f990562.44d8ea",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "out 1",
        "active": false,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 840,
        "y": 100,
        "wires": []
    },
    {
        "id": "fe502aa1.ee37c8",
        "type": "change",
        "z": "1ab3bd40d14573ed",
        "name": "to number",
        "rules": [
            {
                "t": "set",
                "p": "payload[1]",
                "pt": "msg",
                "to": "$number(msg.payload)\t",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1090,
        "y": 580,
        "wires": [
            [
                "d6f23ad6.490eb8"
            ]
        ]
    },
    {
        "id": "9052efa8.e5f0a",
        "type": "switch",
        "z": "1ab3bd40d14573ed",
        "name": "route",
        "property": "parts.index",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 710,
        "y": 260,
        "wires": [
            [
                "2f990562.44d8ea",
                "c2ce6b8c765f31cc"
            ],
            [
                "35825546.228baa"
            ]
        ]
    },
    {
        "id": "35825546.228baa",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "out 2",
        "active": false,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 860,
        "y": 400,
        "wires": []
    },
    {
        "id": "bc23045.f5103f8",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "source data",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "x": 450,
        "y": 600,
        "wires": []
    },
    {
        "id": "3ba2aff1.e8af5",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "spltted",
        "active": false,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "true",
        "targetType": "full",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 680,
        "y": 500,
        "wires": []
    },
    {
        "id": "d6f23ad6.490eb8",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "converted",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "true",
        "targetType": "full",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1390,
        "y": 580,
        "wires": []
    },
    {
        "id": "c2ce6b8c765f31cc",
        "type": "split",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "splt": "_",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 910,
        "y": 240,
        "wires": [
            [
                "b37d5ea810ae500e",
                "d26639cda9cc46fb"
            ]
        ]
    },
    {
        "id": "81c9a0927cc47837",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "Feldnummer",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1660,
        "y": 340,
        "wires": []
    },
    {
        "id": "ab6aafad6bfcafec",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "true",
        "targetType": "full",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1310,
        "y": 200,
        "wires": []
    },
    {
        "id": "05e66712e76b09f5",
        "type": "switch",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "$split(msg.payload,\"P\" )",
                "vt": "jsonata"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 1490,
        "y": 260,
        "wires": [
            []
        ]
    },
    {
        "id": "b37d5ea810ae500e",
        "type": "switch",
        "z": "1ab3bd40d14573ed",
        "name": "separator",
        "property": "parts.index",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 1120,
        "y": 260,
        "wires": [
            [
                "ab6aafad6bfcafec",
                "6f51716ebffcbe6a"
            ],
            [
                "0f8f4bed7a12472e"
            ]
        ]
    },
    {
        "id": "0f8f4bed7a12472e",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "true",
        "targetType": "full",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1310,
        "y": 380,
        "wires": []
    },
    {
        "id": "d26639cda9cc46fb",
        "type": "debug",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "true",
        "targetType": "full",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1110,
        "y": 200,
        "wires": []
    },
    {
        "id": "6f51716ebffcbe6a",
        "type": "split",
        "z": "1ab3bd40d14573ed",
        "name": "",
        "splt": "P",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 1320,
        "y": 280,
        "wires": [
            [
                "05e66712e76b09f5",
                "81c9a0927cc47837"
            ]
        ]
    }
]

```

 ![splitnode1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f6f7071a00ec8b6b1ab519bfeed3b77a07dd4ed4.jpeg)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 December 2021 13:09 UTC](https://discourse.nodered.org/t/split-msg-payload-p-131-to-letters-and-numbers/55429/2 "2021-12-19T13:09:20Z")

</div>

Looking at your flow this is definitely one time I would recommend a single function node with a regex or plain ole JavaScript.

Do you know how to do that?

---

<div class="post-metadata">

**Author:** ![olby2](https://avatars.discourse-cdn.com/v4/letter/o/85f322/32.png) [@olby2](https://discourse.nodered.org/u/olby2)\
**Post date:** [19 December 2021 13:25 UTC](https://discourse.nodered.org/t/split-msg-payload-p-131-to-letters-and-numbers/55429/3 "2021-12-19T13:25:49Z")

</div>

> [@Steve-Mcl](#):
>
> regex

thanks for your quick reply  
I have read "regex" in the description of jsonata, but cannot yet implement it

olby2

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 December 2021 13:30 UTC](https://discourse.nodered.org/t/split-msg-payload-p-131-to-letters-and-numbers/55429/4 "2021-12-19T13:30:56Z")

</div>

Here is a working demo...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/3/93535d0a6dfaf39f49a1f931a69bfcc820558909.png)

Flow (CTRL+I to import)

```auto
[{"id":"50e54099284d1dd1","type":"inject","z":"1ab3bd40d14573ed","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":"","topic":"","payload":"P-131_SPO=-200W","payloadType":"str","x":1940,"y":100,"wires":[["99794fc892202adf"]]},{"id":"99794fc892202adf","type":"function","z":"1ab3bd40d14573ed","name":"","func":"const regex = /([-\\d]*)_(\\w*)=([-\\d]*)(\\w*)/gm;\nconst str = msg.payload\nlet m;\nlet result;\n\nwhile ((m = regex.exec(str)) !== null) {\n // This is necessary to avoid infinite loops with zero-width matches\n if (m.index === regex.lastIndex) {\n regex.lastIndex++;\n }\n result = m;\n}\nif (result && result.length == 5) {\n msg.payload = {\n v1: Number(result[1]),\n v1UOM: result[2],\n v2: Number(result[3]),\n v2UOM: result[4],\n }\n return msg;\n} else {\n node.warn(`cannot parse '${str}'`);\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2140,"y":100,"wires":[["6a848f4a0f89af00"]]},{"id":"6a848f4a0f89af00","type":"debug","z":"1ab3bd40d14573ed","name":"converted","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":2130,"y":160,"wires":[]}]

```

And this is where I tested the regex...

> **[regex101: build, test, and debug regex](https://regex101.com/r/wREm5i/3)**
>
> Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java. Features a regex quiz & library.

---

<div class="post-metadata">

**Author:** ![olby2](https://avatars.discourse-cdn.com/v4/letter/o/85f322/32.png) [@olby2](https://discourse.nodered.org/u/olby2)\
**Post date:** [19 December 2021 14:13 UTC](https://discourse.nodered.org/t/split-msg-payload-p-131-to-letters-and-numbers/55429/5 "2021-12-19T14:13:06Z")

</div>

> [@Steve-Mcl](#):
>
> `const regex = /([-\\d]*)_(\\w*)=([-\\d]*)(\\w*)/gm`

thanks a lot for your quick help. this works for this string exactly as I imagined.  
I also played something more complicated and it probably doesn't work with "\<\>" and floating point numbers are also a problem

I have the formatting on ([- \ d] \*) \_ (\ D \*) = ([- \ d /.] \*) (\ w \*)  
changed and can now also process this "P131\_ SPO = -200.87W"  
Many Thanks  
, that helped me alot  
olby2

---

<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:** [2 January 2022 14:13 UTC](https://discourse.nodered.org/t/split-msg-payload-p-131-to-letters-and-numbers/55429/6 "2022-01-02T14:13:47Z")

</div>

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