用于匹配未跟随某个其他子字符串的子字符串的正则表达式

Ray*_*yne 102 java regex clojure

我需要一个匹配blahfooblah但不匹配的正则表达式blahfoobarblah

我希望它只匹配foo和foo周围的所有东西,只要它没有跟着bar.

我尝试使用它:foo.*(?<!bar)它非常接近,但它匹配blahfoobarblah.背后的负面看法需要匹配任何东西而不仅仅是酒吧.

我正在使用的特定语言是Clojure,它使用Java正则表达式.

编辑:更具体地说,我也需要通过blahfooblahfoobarblah但不是blahfoobarblahblah.

mač*_*ček 142

尝试:

/(?!.*bar)(?=.*foo)^(\w+)$/
Run Code Online (Sandbox Code Playgroud)

测试:

blahfooblah            # pass
blahfooblahbarfail     # fail
somethingfoo           # pass
shouldbarfooshouldfail # fail
barfoofail             # fail
Run Code Online (Sandbox Code Playgroud)

正则表达式解释

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    bar                      'bar'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    foo                      'foo'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Run Code Online (Sandbox Code Playgroud)

其他正则表达式

如果你只想bar在它之后直接排除foo,你可以使用

/(?!.*foobar)(?=.*foo)^(\w+)$/
Run Code Online (Sandbox Code Playgroud)

编辑

您对问题进行了更新,以使其具体化.

/(?=.*foo(?!bar))^(\w+)$/
Run Code Online (Sandbox Code Playgroud)

新的测试

fooshouldbarpass               # pass
butnotfoobarfail               # fail
fooshouldpassevenwithfoobar    # pass
nofuuhere                      # fail
Run Code Online (Sandbox Code Playgroud)

新的解释

(?=.*foo(?!bar))确保foo找到a但未直接遵循bar


ste*_*son 49

foo通过不能开头的内容匹配以下内容bar,请尝试

foo(?!bar)
Run Code Online (Sandbox Code Playgroud)

具有负面外观背后的版本实际上是"匹配foo后跟不会结束的内容bar".本.*场比赛所有的barblah,而(?<!bar)回看lah,并检查不匹配bar,它没有,所以整个模式匹配.