Question

I want to ask about back reference in egrep.

I have a file, it contains:

aa aa someothertext

and there are something like 77 77

How do I use back reference to match the pattern 'aa aa' and '77 77'?

I tried:

egrep '(aa )\1' file.txt

and it will match 'aa aa'. Then. I tried to replace 'aa' with ' ([a-zA-Z0-9])\1', which yields:

egrep '(([a-zA-Z0-9])\1 )\1' file.txt

It won't work.

I'd appreciate if you can help!

Was it helpful?

Solution

Remember that capturing groups are indexed by their opening parenthesis: you were calling the first group before it was defined.

In ((a)b), \1 is referring to (a)b and \2 to a.

To fix this, you can use the correct index:

(([a-zA-Z0-9])\2 )\1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top