一个正则表达式来匹配后面没有某个其他子字符串的子字符串 [英] A regex to match a substring that isn't followed by a certain other substring

查看:23
本文介绍了一个正则表达式来匹配后面没有某个其他子字符串的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个匹配 blahfooblah 但不匹配 blahfoobarblah

I need a regex that will match blahfooblah but not blahfoobarblah

我希望它只匹配 foo 和 foo 周围的所有内容,只要它后面没有 bar.

I want it to match only foo and everything around foo, as long as it isn't followed by bar.

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

I tried using this: foo.*(?<!bar) which is fairly close, but it matches blahfoobarblah. The negative look behind needs to match anything and not just bar.

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

The specific language I'm using is Clojure which uses Java regexes under the hood.

更具体地说,我还需要它通过 blahfooblahfoobarblah 而不是 blahfoobarblahblah.

More specifically, I also need it to pass blahfooblahfoobarblah but not blahfoobarblahblah.

推荐答案

尝试:

/(?!.*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 
 (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 
 (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 
, and the end of the
                           string

其他正则表达式

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

Other regex

If you only want to exclude bar when it is directly after foo, you can use

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

<小时>

编辑

您更新了您的问题以使其具体.


Edit

You made an update to your question to make it specific.

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

新测试

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

新解释

(?=.*foo(?!bar)) 确保找到了 foo 但不直接跟随 bar

New explanation

(?=.*foo(?!bar)) ensures a foo is found but is not followed directly bar

这篇关于一个正则表达式来匹配后面没有某个其他子字符串的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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