[General boards] [Fall 2018 courses] [Summer 2018 courses] [Winter 2018 courses] [Older or newer terms]

Regex question from slides


#1

^(([ab])c)\2\1$

what is the first group and second group here?
I don’t get how acaac and bcbbc matches this expression


#2

I believe the trick is with the positioning of the parantheses so first what the regex checks for is an expression which contains “a” or “b” followed by a “c”. If you notice the way the parantheses are placed ((ab)c) is the first part and (ab) is within it. So then it’s checking for the second part followed by the first. So ac then just a then ac again which gives you “acaac” is valid. Similarly, bc then just b then bc again which gives you “bcbbc”.


#3

thanks for answering, I get it now


#4

Groups are numbered in the same order as their open parenthesis appears from left to right.

So (a(b©)) has three groups. \1 refers to the entire expression. \2 refers to the bc and \3 is just c.