Shame on me… I do not get it to put a REGEX expression into an xtend transform command.
I have a string which contains number and characters and I want to get the numbers only as 1 number
the required regex expression is [0-9]+
but any attempt to embed it into a transform command fails
var String strCommand2 = transform("REGEX","[0-9]+",strCommand)
I have tried it with additional /( and various combinations without luck.
Could somebody please help me out?
Hi Udo,
many thanks. You probably mean the implementation of regex in OH does not support that because the regex itself returns the desired value in any expression builder?
Hi Wolfgang,
my problem is not to find the correct regex expression. the problem is, to get it working in a DSL rule.
Your expression works on regex101.com, but not if you put it into a DSL rule.
And your expression only works if there are characters left from a number, (\d+) does not care even if there are characters left and right from a number.
With one exception. OH REGEX requires the pattern to match the entire string, including new lines and such. Then it will return only the first matching group (parens).
That trips of a lot of people.
@Oliver2, given the restrictions with the REGEX transform I’ve found the best way to construct the pattern is to find markers immediately before the string you want and markers immediately after the string you want and create a pattern along the lines of:
.*M(.*)N.*
where M is the marker before the string you want and N is the marker after the string you want.
So if your Item state follows the pattern of any number of digits followed by a letter, something like ([0-9]+)[a-zA-Z].* should do it. Note that the + is inside the parens.
Rich,
great that you help.
I have spent half a day and am desperate as I cannot get it working correctly.
Could you please show me the correct expression for my specific problem?
given string: charactersinfront1234charactersafter
expected result: 1234
amount of characters in front of numbers and after numbers can vary, also amount of number digits.
to say it in english: just get all numbers from that string.