正则表达式:匹配除反向引用以外的所有内容 [英] Regex: Match everything except backreference
本文介绍了正则表达式:匹配除反向引用以外的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下示例行:
a_a
b_c
如何(使用grep / egrep)匹配第一个字母不等于最后一个字母的行?我尝试了以下方法,但这似乎不起作用。
egrep([ab])_ [^ \ 1 ]
使用egrep -v或反向正则表达式(匹配除第一个字母等于最后一个字母之外的所有内容)对于我的用例,不可能。
使用组匹配您创建了最后加上组(
\ 1
)。 c> ^(。)。* \ 1 $ egrep'^(。)。* \ 1 $'
< a href =https://regex101.com/r/dZ4kB6/1 =nofollow> regex101 编辑
^(。)。*((?!\1)。)+ $
regex101
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 correclty.
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 group with 1st character, and then put group (\1
) in the end.
^(.).*\1$
egrep '^(.).*\1$'
regex101
EDIT
Opposite
^(.).*((?!\1).)+$
regex101
这篇关于正则表达式:匹配除反向引用以外的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文