How to match 3 letters word where first letter and third are the same? (regex,egrep)

i need regular expression that matches a word consists of three letters where the first and third letters are identical

for example


You can use the following regular expression to match 3 letter words where the first and third letters are the same:

grep -E '(.).\1` file.txt
# Strict mode starting with  the start of the line (SOL) #
grep -E '^(.).\1` file.txt

Explanation about matching 3 letters words where first lesster and third are the same:

  • ^ matches the start of the line
  • (.) matches any character and captures it in a group
  • . matches any character
  • \1 matches the first captured group (i.e. the first letter)
  • $ matches the end of the line`
1 Like

Where can i learn about \1 ? I don’t see explanation in most tutorials about regular expression

Open terminal and read grep manual page (look for REGULAR EXPRESSIONS section and read Back-references and Subexpressions. From grep(1):

   The back-reference \n, where n is a single digit, matches the substring
   previously matched  by  the  nth  parenthesized  subexpression  of  the
   regular expression.


man grep 
1 Like

@Raj , i thought about it , i have ended with another solution

your solution:
grep -E '(.).\1` file.txt
in this case, it will match lines
aaa bbb cc
will also work

the dot β€œ.” gives any character (it may be a letter, or a number digit, or a symbol)
[a-zA-Z] would give exactly a letter

and must be started with ^ and end ended with $ to limit it to exactly three letters
egrep β€œ^([a-zA-Z])[a-zA-Z]\1$” filename