如何使用正则表达式找到除某些短语之外的所有内容? [英] How can I find everything BUT certain phrases with a regular expression?

查看:35
本文介绍了如何使用正则表达式找到除某些短语之外的所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有一个短语foo bar",我想找到除foo bar"之外的所有内容.
这是我的正文.

Ok, so I have a phrase "foo bar" and I want to find everything BUT "foo bar".
Here's my text.

ipsum dolor foo bar Lorem ipsum dolor 坐 amet,
consectetur adipisicing 精英, sed do
eiusmod tempor foo bar incididunt ut Labore et
多洛尔富酒吧

ipsum dolor foo bar Lorem ipsum dolor sit amet,
consectetur adipisicing elit, sed do
eiusmod tempor foo bar incididunt ut labore et
dolore foo bar

有一种方法可以在正则表达式中做到这一点,对吗?我不必去使用字符串等,对吗?

There's a way to do this just within regex right? I don't have to go and use strings etc. do I?

结果:

注意我不能很好地突出显示,但粗体给了你一个想法(虽然前后的空格也会被选中,但它打破了粗体).

NOTE I can't do a nice highlighting but the bold gives you an idea (although the spaces that are before and after would also be selected but it breaks the bolding).

ipsum dolor foo bar Lorem ipsum dolor sat amet,
consectetur adipisicing 精英,sed 做
eiusmod tempor foo bar incididunt ut Labore et
dolore foo bar

假设 PCRE 命名法.

2013 年 7 月 29 日更新:最好使用您选择的语言的搜索和替换功能来删除"您不想要的短语,这样您就可以得到您所做的信息想要.

UPDATE 7/29/2013: it may be better to use a search and replace function in your language of choice to just 'remove' the phrases that you don't want so that you are then left with the info you do want.

推荐答案

try

^(?!.*foo bar).*$

这应该选择不包含foo bar"的每一行.(?! = 负前瞻)

this should select every line that does not contain "foo bar". (?! = negative lookahead)

这篇关于如何使用正则表达式找到除某些短语之外的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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