I have the following example lines:
a_a
b_c
How (using grep
/egrep
) would I match the lines where the first letter
Use group matches.
There you create a group with the 1st character, and then put group (\1
) at the end.
^(.).*\1$
egrep '^(.).*\1$'
Opposite:
^(.).*((?!\1).)+$
pcregrep '^(.).*((?!\1).)+$'
Use a negative look behind anchored to end of input. A general regex for "first char is not last char" is:
^(.).*(?<!\1)$
To match only your type of input:
^(.)_.(?<!\1)$
May be you are looking for this:
\b([a-z])\w+(?!(\1))([a-z])\b
it works for:
a_a and b_c
(tested with: The Regulator 2.0.3)
You can then adjust this regex to meet further your needs