Perfect_Comment Perfect_Comment - 1 year ago 87
Groovy Question

Unsure why this regex pattern prints out the result it does

I am attempting to learn regex and have come across a pattern that i am having trouble with - i am unable to figure how it is works exactly. What is the value of x? The language is Groovy.

x=(("World Hotel =~ /^(W....)\s+H(.*)$/)[0][2])

I can follow the regex itself but the values in the final 2 square brackets are a mystery to me. The value printed is 'otel', can someone please explain why this is?

Answer Source

Note that the regex ends at the /. So we have the expression

"World Hotel =~ /^(W....)\s+H(.*)$/

which matches the string World Hotel against the regex ^(W....)\s+H(.*)$. This will place World into the first group, and otel into the second capturing group. The first capturing group requires an upper-case W at the very start of the string, followed by four arbitrary characters; after that whitespace is matched to an arbitrary non-zero length, followed by an upper-case H; the last capturing group then matches the rest of the string until its end.

Having never used Groovy I can only speculate that the index expressions afterwards [0] and [2] will retrieve the first (and only) match ([0]) and the second capturing group of that match ([2]), respectively. Which would explain the result you're seeing.

Also you seem to have one closing parenthesis too much.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download