I am trying to decode some groovy script. I was able to figure out that it is a regular expression but couldn't figure out what the code is exactly.
def dirNumber = this.'Directory Number'
dirNumber?"61" + (dirNumber =~ /0([0-9]+)/)[0][1] + "@":null
I am trying to decode some groovy script. I was able to figure out that it is a regular expression but couldn't figure out what the code is exactly.
def dirNumber = this.'Directory Number'
dirNumber?"61" + (dirNumber =~ /0([0-9]+)/)[0][1] + "@":null
According to Regular expression operators section of https://groovy-lang.org/operators.html,
=~is the find operator, which creates ajava.util.regex.Matcherfor pattern to the right matching them on string to the left. So,dirNumber =~ /0([0-9]+)/is equivalent toPattern.compile("/0([0-9]+)/").matcher(dirNumber)and evaluates to an instance ofjava.util.regex.Matcher.Groovy gives you the ability to access matches by index (
[0]in your code); your regular expression uses grouping, so in each match you can access groups by (1-based: 0 denotes the entire pattern) index ([1]in your code), too.So, your code (if
dirNumberis notnull) extracts the first group of the first match of the regular expression.// EDITED
You can get an
IndexOutOfBoundsExceptionwhen the first index ([0]in your code) is out of the matches' range; when your second index ([1]in you code) is of the grous' range, you get anullwithout exception accessing the group through index...You can test these scenarios with a simplified version of the code above: