Grep backreference questions
In your regex, (blue|blue)
is a capturing group. After a capturing group has captured a group, \1
can be used to refer to what was captured. (That's why it's called capturing: because it holds onto it so you can use it later.)
If you had a second parenthesized capturing group, you could use \2
to refer to it, or \3
for a third, and so on.
So the regex matches "blue", then any characters, then "blue" again. That's why it doesn't match the first two lines.
When you change it to (blue|green)
, it matches either "blue" or "green", then any characters, then whatever the capturing group matched. So it matches either of these:
there is a blue and blue beach ball
there is a green and green beach ball
but it does not match either of these:
there is a blue and green beach ball
there is a green and blue beach ball
If you want to match those, you will need to use another group (does not matter if it's capturing or not), like this: grep -E '(blue|green).*(blue|green)'