【问题标题】:Regex select all BUT group正则表达式选择所有但组
【发布时间】: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


【解决方案1】:

这是一种方法:(demo)

\W*\b(?!foobar).+?\b\W*

.+? 中的? 是为了确保我们在得到\b 后立即停止匹配,否则我们可能会跳过一些foobar

\W* 是使用字符串中任何前导或尾随非单词字符所必需的。

这里每个单词和每个单词分隔符都是单独匹配的,可能不太理想。


Full explanation:

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  \W*                      non-word characters (all but a-z, A-Z, 0-
                           9, _) (0 or more times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    foobar                   'foobar'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  .+?                      any character except \n (1 or more times
                           (matching the least amount possible))
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  \W*                      non-word characters (all but a-z, A-Z, 0-
                           9, _) (0 or more times (matching the most
                           amount possible))

后视和前瞻的变体:(使用/gs/gm)(demo

(?<=^|\bfoobar\b)(?!foobar\b)(.*?)(?=\bfoobar\b|$)

我相信所有这些\b 都是正确处理foobar 作为单词一部分出现的所有情况所必需的(如果它作为单词的一部分也应该被排除在外,只需删除所有\b应该工作)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-18
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 2016-01-11
    相关资源
    最近更新 更多