Filter certain text from string

I have this payload that I want to filter:

Wallet height: 760264
Sync status: Synced
Balances, fingerprint: 3007886911
Wallet ID 1 type STANDARD_WALLET
   -Total Balance: 2958.5843702 xcc (295858437020 mojo)
   -Pending Total Balance: 2958.5843702 xcc (295858437020 mojo)
   -Spendable: 2958.5843702 xcc (295858437020 mojo)

The only text I want is part of the total balance line:

2958.5843702 xcc

The number will change each time I run the command.
Is there an easy way to achieve this?

If any easier, I could just have the whole number:

2958 xcc

TIA

Hi ..

you could use the javascript split() method on the string. it is a string right ?

first split on "-Total Balance: " and then on the next space and bracket " ("

msg.payload = msg.payload.split("-Total Balance: ")[1].split(" (")[0]

Test Flow:

[{"id":"4167eff6f372de2a","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":180,"y":560,"wires":[["1afa79ba49f417ca"]]},{"id":"20a4b7f8e898d211","type":"function","z":"54efb553244c241f","name":"","func":"\nmsg.payload = msg.payload.split(\"-Total Balance: \")[1].split(\" (\")[0]\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":560,"wires":[["1f8cf867e41c3b5f"]]},{"id":"1f8cf867e41c3b5f","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":560,"wires":[]},{"id":"1afa79ba49f417ca","type":"template","z":"54efb553244c241f","name":"text","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Wallet height: 760264\nSync status: Synced\nBalances, fingerprint: 3007886911\nWallet ID 1 type STANDARD_WALLET\n   -Total Balance: 2958.5843702 xcc (295858437020 mojo)\n   -Pending Total Balance: 2958.5843702 xcc (295858437020 mojo)\n   -Spendable: 2958.5843702 xcc (295858437020 mojo)","output":"str","x":330,"y":560,"wires":[["20a4b7f8e898d211"]]}]
1 Like

Exactly what I wanted, many thanks.

You could also use a simpler (but harder to read) regular expression:

// exec returns null if not found, or array of what matched the pattern if any are found: ["2958.5843702"]
const matches = /(?<=-Total Balance: )[\d\.]+/.exec(msg.payload);

// If not null, grab the first matching string and parse it as a number:
if (matches) msg.payload = parseFloat(matches[0])
2 Likes

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