Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was doing some exercises on regular expressions, but I can't seem to be able to crack this one :

egrep in a file where the 2nd and 3rd character are the same.

I tried :

egrep  '^..{2}' /usr/share/dict/dutch

However this is wrong, how should I match the 2nd and 3rd character in a regex? How would I match the 2nd and 4th character in a regex (not an exercise, but I would like to know this one as well.)?

share|improve this question
up vote 3 down vote accepted

You'll need backreferences for the first part:

egrep '^.(.)\1'

The .{2} simply matches any two characters.

If you want the 2nd and 4th characters the same, it's the same idea as above:

egrep '^.(.).\1'
share|improve this answer

Use backreferences. The answer to your first question (2nd, 3rd char same) is:

egrep '^.(.)\1'

http://www.regular-expressions.info/brackets.html is a simple intro to backreferences.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.