[SOLVED] Too dumb to use regex?

Hi everybody,

i think i am missing something when using REGEX as transformation within rules.
I have the following string “Somebodys Phone (home)”.

I want to remove everything within the parentheses, so the result should be “Somebodys Phone”.

On regex101.com the following works:

[\w\s]+[^(\w)]

In my rule, I have the following:

var teststring = "Somebodys phone (home)"
var regexpattern = "[\\w\\s]+[^(\\w)]"
var result = transform("REGEX", regexpattern, teststring )
logInfo("Regex result", result)

but the variable result is always NULL.

I think i’ve missed something simple, but i can’t find what…

Thanks for helping :slight_smile:

Do you have the regex transformation installed?

Yes. Version 2.3.

OK i did some more tests.
I’ve tried with

.+

as regex and OpenHab complained that the regex

^.+$

does not contain a capture group and therefore nothing will be returned.
So the RegEx transformation puts ^ at the beginning of the expression and $ to the end.
With both characters i’m having troubles find a regex that does what I want.
Remove all word within parentheses plus the parentheses themselves from a string.

I’m trying to find the correct expression with regex101, but can somebody give me a hint?

I do know that the regex in OH often works differently from what works at that web site.

For one thing, you need to escape the parens or else it will treat that as the part of the string you want to return.

Try making the regex a little looser.

(.*)\(.*\)

This will return everything except what is between the parens.

If that works you can tighten the expression up a bit so it only matches letters and spaces.

([\\w\\s]+)\([\\w]+\)

@rlkoshak Thanks a lot, you are my man!
With

(.*)\(.*\)

it is working!
Thanks a lot - i don’t think i need to tighten it., since I never can be sure what comes before the string in ().
I’m letting Alexa tell me who’s calling me and therefore I don’t want:
Incoming call from Sascha Mobile (Mobile)

So and since my Wife also adds contacts to the phonebook, i can’t tighten it :wink: