I’m having a string of hex numbers like this:
0C 72 00 ED 4D
The numbers should be extracted and assigned to items.
My first approach works and looks like this:
sString = serialString.state.toString.trim
sRegex = "([0-9A-F]{2})\\s?([0-9A-F]{2})\\s?([0-9A-F]{2})\\s?([0-9A-F]{2})\\s?([0-9A-F]{2})\\s?"
pattern = Pattern::compile(sRegex)
matcher = pattern.matcher(sString)
matcher.find()
item1= Integer::parseInt(matcher.group(1), 16)
item2= Integer::parseInt(matcher.group(2), 16)
...
That’s not very nice and things will become difficult if the lenght of the hex string changes.
So I did some research to see how repeating pattern can be matched.
In Java something like this should work:
sString = serialString.state.toString.trim
sRegex = "([0-9A-F]{2})"
pattern = Pattern::compile(sRegex)
matcher = pattern.matcher(sString)
while( matcher.find() ) {
logInfo("patternMatcher", matcher.groupCount() + " " + matcher.group() )
}
However, this doesn’t work here: Only the first group is found.
Modifiers that work well with a regex tester result in errors when used in a rule:
The /myRegex/g usually returns all matching groups of a string.
So my questions is how can I find all groups of the string in a rule?
Is there an elegant way like in Java?