I want regex to match a pattern only when it consists of a repeated subpattern. It can be boiled down to something as simple as the following. Given this text:
a a
a b
b b
I want a regex pattern that will only match "a a" and "b b" (and NOT "a b"), because there are two occurrences of the same subpattern on those lines.
I'm working in BBEdit, though the solution presumably would apply to any extended regex. I've been reading a lot about conditional subpatterns here on stack overflow and elsewhere, and experimenting as I go, but I don't seem to be able to make it work. Probably going to be chagrinned when I find out how simple it is. Bonus points (as usual) for explaining why the regex works the way it does.
Well from your example data, you want to use a Backreference like so..
(.) \1
Explanation:
Backreferences allow you to refer back to what was previously matched by a capturing group.
A backreference is specified in the regular expression as a backslash (\) followed by a number indicating the number of the capturing group to be recalled.
( # group and capture to \1:
. # any character except \n
) # end of \1
# ' '
\1 # what was matched by capture \1
Live Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With