Heyo,
I got a little problem with the RegEx transformation service
The first capturing group becomes the result of the transformation
But I dont want that. I want to get the second capturing group or the third and so on.
Example:
I got my item, grep the name and I want to build a String with given item.
C2SM01
First RegEx would be
var getDir = transform("REGEX", "(\\w\\d+).*", item.name().toString())
and the second would be
var getPDF = transform("REGEX", "(\\w\\d+)(\\w+\\d+).*", item.name().toString())
but I can’t get the 2nd group.
Is there a possibility to get the 2nd group?
If no is there an alternative way?
5iver
(Scott Rushworth)
2
You know the value of the first group, so you can use it:
var getPDF = transform("REGEX", getDir + "(\\w+\\d+).*", item.name.toString)
1 Like
Okay maybe I was not clear.
var getDir = transform("REGEX", "(\\w\\d+).*", item.name().toString())
gets C2 FROM C2SM01
Your RegEx will give the full name
var getPDF = transform("REGEX", getDir + "(\\w+\\d+).*", item.name.toString)
getPDF: C2SM01
BUT I want SM01
EDIT
MY MISTAKE! I did not read your regex correctly! SORRY! Yours is correct! Thank you!
5iver
(Scott Rushworth)
4
Nah, did you try it?
C2(\w+\d+).*
Finds literal C2, then matches the first capturing group, which would be SM01.
1 Like
with yours it would be this RegEx ^C2(\w+\d+).*$
var getDir = transform("REGEX", "(\\w\\d+).*", item.name().toString())
var getPDF = transform("REGEX", getDir + "(\\w+\\d+).*", item.name.toString)
with matches the item.name() C2SM01 which delivers SM01
Side note: As I believe you are using openHAB 2, you should refer to the openHAB 2 documentation first. http://docs.openhab.org/addons/transformations/regex/readme.html