正则表达式:匹配除反向引用之外的所有内容 [英] Regex: Match everything except backreference

查看:16
本文介绍了正则表达式:匹配除反向引用之外的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例行:

a_a
b_c

如何(使用 grep/egrep)匹配第一个字母不等于最后一个字母的行?我尝试了以下方法,但这似乎无法正常工作.

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]

使用 egrep -v 或反向正则表达式(匹配除第一个字母等于最后一个字母之外的所有内容)对于我的用例不可能.

Working with egrep -v or the inverse regex, (match everything except where first letter equals last letter), is not possible for my use case.

推荐答案

使用组匹配.

在那里你用第一个字符创建一个组,然后把组 (1) 放在最后.

There you create a group with the 1st character, and then put group (1) at the end.

^(.).*1$

egrep '^(.).*1$'  

相反:

^(.).*((?!1).)+$

pcregrep '^(.).*((?!1).)+$'

这篇关于正则表达式:匹配除反向引用之外的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆