I have the following example lines:
a_a
b_c
How (using grep
/ egrep
) would I match the lines where the first letter is not equal to the last letter? I have tried the following but this does not seem to work correctly.
egrep ([ab])_[^\1]
Working with egrep -v
or the inverse regex, (match everything except where first letter equals last letter), is not possible for my use case.
Use group matches.
There you create a group with the 1st character, and then put group ( \\1
) at the end.
egrep '^(.).*\1$'
Opposite:
pcregrep '^(.).*((?!\1).)+$'
May be you are looking for this:
\\b([az])\\w+(?!(\\1))([az])\\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
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)$
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.