[SOLVED] Simple REGEX transformation

I have a json response:
RE-KL xxxx (XC60/2017) 23126km (fuel 24% 240km)
position: xx.63571166992188,xx.92348480224609
locked: yes
heater: off

I successfully extract the heater state:
val String heater = transform("REGEX", ".*heater: (.*)", json)
–> off

When trying this with the locked state:
val String lock = transform("REGEX", ".*locked: (.*).*", json)
I get:
heater: off

With a test at https://regex101.com/ everything is fine.
I tried also:

".*locked: (.*).*$"
".*locked: (yes|no)"
"locked: (.*?)$.*

So how to ignore everything after the match “yes” ?

I have seen something about global meaning that it’s not stopping after the first match.
So I tried “.locked: (.)/g”
–> null

you have to address the newlines in your response, i think each line is matched against the regex and if it does not match you get the line back, see:

But if you really ahve a valid json a json transformation would be a better approach.

Thanks for your quick response.

Actually it’s no real json response - sorry for that mistake (no brackets like { or [).
I thought so to check for new lines and I also tried \r\n and \n without a result.
If it’s true that each single line will be checked, why doesn’t REGEX not return the first and second line?

locked: (yes|no)\n
returns the entire String
same with
\nlocked: (yes|no).*\n

\nlocked: (.).\n
returns the entire string as well.

Have a closer look to the linked post and use \s to skip the newlines.

I will, thanks

\s is Invalid escape sequence.
I tried \s then in various versions:

//	val String lock = transform("JSONPATH", ".*\\s.*locked: (.*)\\s.*", json) -> returns entire string
// val String lock = transform("JSONPATH", ".*\n.*\n.*locked: (.*)\n.*", json) -> returns entire string
//	val String lock = transform("JSONPATH", ".*\\s.*\\s.*locked: (.*)\\s.*", json) -> returns entire string
	val String lock = transform("JSONPATH", ".*locked: (.*)\\s.*", json)

But these returned the entire string.

I did not use JS yet, but will give it a try.

Can you get a proper json instead or is this a fixed response?
A Javascript transform may be the easier way in your case.

I would love to use json, but the response is exactly the format above (fixed response)

REGEX uses a greedy search. So the heater one works because there is nothing after the “off” on that line. But for locked, it is including the heater line as well because .* matches any character, including new lines and spaces and such and you didn’t give it anything to stop the match. So, assuming that the text is always of this format

transform("REGEX", ".*locked: (.*)[\\s]+.*", json)

should work.

the first .* matches everything up to "locked: ".

The (.*) is the part that we want returned and will match everything up to the [\s]+ which matches one or more whitespace characters which includes newlines.

The final .* matches the rest of the string. I’ve found that you will have the best results if your REGEX matches the whole String. I’ve not had good luck using \g to do that.

If that fails, you have help it by adding the heater line to the expression:

transform("REGEX", ".*locked: (.*)[\\s]+heater.*", json)

Got it:

val String lock = transform("REGEX", ".*locked: (.*).*\n.*heater:.*", json)

This returns


This somehow returns:
" so still including a newline.

I got a working solution.

Thanks for all your help.