Regex: Match everything except backreference

后端 未结 3 2049
南旧
南旧 2021-01-14 12:49

I have the following example lines:

a_a
b_c

How (using grep/egrep) would I match the lines where the first letter

相关标签:
3条回答
  • 2021-01-14 12:55

    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).)+$'
    
    0 讨论(0)
  • 2021-01-14 12:58

    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)$
    
    0 讨论(0)
  • 2021-01-14 13:00

    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

    0 讨论(0)
提交回复
热议问题