I'm trying to follow the example of Documenting Regular Expressions in Groovy but can't get my own example to work. Here's an example that fails on regex1, but works on the compressed regex2
def line = "some.key=a value # with comment that is ignored"
def regex1 = '''(?x) # enable extended patterns
^\\s* # ignore starting whitespace
([^=#]+) # capture key
= # literal
([^#]*) # capture value'''
def regex2 = '''^\\s*([^=#]+)=([^#]*)'''
def pattern = ~regex1
def matcher = pattern.matcher(line)
for (i=0; i < matcher.getCount(); i++) {
println matcher[i][0]
println matcher[i][1]
println matcher[i][2]
}
The error i'm getting is
Caught: java.util.regex.PatternSyntaxException: Unclosed character class near index 217`
which points to the final closing brace on the last match.
If i change regex2 and add (?x) to the start of the string, it too fails in same way.
What is the correct syntax is for adding the extended patterns in this case? The example on the linked site works fine, so I know it should be possible.