i need regular expression that matches a word consists of three letters where the first and third letters are identical
for example
aba
cnc
kak
i need regular expression that matches a word consists of three letters where the first and third letters are identical
for example
aba
cnc
kak
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
^
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`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.
Type:
man grep
@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
caca
dada
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