一个正则表达式,用于匹配不后跟某个其他子字符串的子字符串

2022-08-31 08:11:57

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

我希望它只匹配 foo 和 foo 周围的一切,只要它后面没有 bar。

我尝试使用这个:它相当接近,但它匹配。背后的负面表情需要与任何东西相匹配,而不仅仅是酒吧。foo.*(?<!bar)blahfoobarblah

我使用的特定语言是Clojure,它在引擎盖下使用Java正则表达式。

编辑:更具体地说,我还需要它通过,但不是。blahfooblahfoobarblahblahfoobarblahblah


答案 1

尝试:

/(?!.*bar)(?=.*foo)^(\w+)$/

测试:

blahfooblah            # pass
blahfooblahbarfail     # fail
somethingfoo           # pass
shouldbarfooshouldfail # fail
barfoofail             # fail

正则表达式说明

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

其他正则表达式

如果只想在直接之后排除,可以使用barfoo

/(?!.*foobar)(?=.*foo)^(\w+)$/

编辑

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

/(?=.*foo(?!bar))^(\w+)$/

新测试

fooshouldbarpass               # pass
butnotfoobarfail               # fail
fooshouldpassevenwithfoobar    # pass
nofuuhere                      # fail

新解释

(?=.*foo(?!bar))确保找到 A 但不直接遵循foobar


答案 2

要将以下内容与不以 开头的内容相匹配,请尝试foobar

foo(?!bar)

你带有负面外观的版本实际上是“匹配后跟不以结尾的东西”。匹配所有 ,并且回顾并检查它不匹配,它不匹配,所以整个模式匹配。foobar.*barblah(?<!bar)lahbar