Transform regex replace

Hi all,
I have following strings like:

Es treten Windböen mit Geschwindigkeiten zwischen 50 km/h (14m/s, 28kn, Bft 7) und 60 km/h (17m/s, 33kn, Bft 7) aus südwestlicher Richtung auf. In Schauernähe muss mit Sturmböen um 70 km/h (20m/s, 38kn, Bft 8) gerechnet werden.

And I want to replace:

  • km/h with Kilometer pro Stunde

And remove:

  • (14m/s, 28kn, Bft 7)
  • (17m/s, 33kn, Bft 7)
  • (20m/s, 38kn, Bft 8)

The best way seems to be a transformation with regex, right? Does anybody have a clue how to do it?

Thanks
Tobias

The best way depends on context.

Where is this String coming from?

Do you need to do this transform before the value gets set to an Item or are you doing this from a Rule?

What binding are you using to get this value?

Oh sure, sorry. I forgot this information.

The string is coming from DWD warnings, as you can see here:
https://community.openhab.org/t/solved-json-path-weather-warnings-dwd-deutscher-wetterdienst/46295

At the rule I post the warnings description to my item:

DWD_Warnung_description.postUpdate(transform("JSONPATH","$.description",newString))

I addition to this, I want to send the warning to Alexa.

AmazonEchoDotWohnzimmer_Speak.sendCommand("Warnung des Deutschen Wetterdienstes" + DWD_Warnung_headline.state.toString + DWD_Warnung_description.state.toString + DWD_Warnung_instruction.state.toString)

Alexa doesnt know km/h so it sounds really weired. I’m looking now for a solution, how to replace eg. km/h with “Kilometer pro Stunde” and removing all content of the ( … ) within this rule.

Best regards
Tobias

You could use two regex expressions and do everything including the jsonpath in a js transformation. It would look something like this (not tested):

(function(i) {
    var message = JSON.parse(i);
    message = message.replace(/\((.*?)\)/g, "").replace(/km\/h/g, "Kilometer pro Stunde");
	return message;
})(input)

Put the regex into regex101.com for an explanation of how they work.
Best regard from Berlin Johannes

1 Like

Great! Thanks a lot! That should work for me!