【发布时间】:2015-08-04 11:26:33
【问题描述】:
所以我处于必须只使用正则表达式来选择除特定单词之外的所有内容的情况。出于示例的目的,该词将是foobar。这是应该发生的事情的一个例子:
this should be highlighted, and
same with this. but any sentence
that has the word
foobar
shouldnt be, and same for any regular
sentence with foobar <-- like that
foobar beginning a sentence should invalidate
the entire sentence, same with at the end foobar
only foobar, and nothing else of the sentence
more words here more irrelevant stuff to highlight
and nothing of the key word
what about multiple foobar on the same foobar line?
什么应该匹配,看起来像这样:
我能得到的最好的结果是/\b(?!foobar)[^\n]+\n?/g,如果单词 foobar 单独出现在它自己的单独行上,它的格式如下:
not foobar
foobar (ignored)
totallynotfoobar
nobar
foobutts
foobar (ignored)
notagain
其余的都匹配了……但这不是我想要的。
所以我的问题是,我将如何完成原始示例?有没有可能?
【问题讨论】:
-
您使用什么语言/工具进行正则表达式?
-
@anubhava 没关系.. 他知道正则表达式是什么,他可以应用它
-
@barlop:根据工具的不同,解决方案可能会有所不同。
-
@nhahtdh 如果你在记事本++中这样做,我相信它很好并且可以适应
-
我猜一个解决方案将涉及负前瞻或负后瞻,尽管我不确定它是什么
标签: regex