将不包含特定文本的行与正则表达式匹配 [英] matching a line that doesn't contain specific text with regular expressions

查看:35
本文介绍了将不包含特定文本的行与正则表达式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用正则表达式执行以下操作,但不确定如何执行.当 one two 是行的开头时,我希望它匹配 one two 除非字符串在 one two<之后的任何位置包含 three/代码>.

I want to do the following with regular expressions but not sure how to do it. I want it to match one two when one two is the beginning of the line unless the string contains three anywhere after one two.

推荐答案

您需要一个 否定前瞻断言 - 类似这样:

You need a negative lookahead assertion - something like this:

/^one two(?!.*three)/m

这是关于前瞻/后视断言的教程

注意:我添加了 'm' 修饰符,以便 ^ 匹配一行的开头而不是整个字符串的开头.

Note: I've added the 'm' modifier so that ^ matches the start of a line rather than the start of the whole string.

这篇关于将不包含特定文本的行与正则表达式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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