一个正则表达式匹配一个子字符串,后面不跟一个特定的其他子字符串 [英] A regex to match a substring that isn't followed by a certain other substring

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

问题描述

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



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



我试过使用这个: foo。*(?<!bar)这是相当接近的,但它匹配 blahfoobarblah



我使用的具体语言是Clojure,它使用了Java正则表达式。



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

解决方案

试用:



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



测试:



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



正则表达式解释



 节点说明
-------------------------- -------------------------------------------------- ----
(?!向前看看是否有没有:
-------------------------- -------------------------------------------------- ----
。*除了\\\
之外的任何字符(0或更多次
(匹配最多的可能))
------------- -------------------------------------------------- -----------------
bar'bar'
--------------------- -------------------------------------------------- ---------
)结束前瞻
--------------------------- -------------------------------------------------- ---
(?=向前看看是否有:
---------------------------- -------------------------------------------------- -
。*除了\\\
之外的任何字符(0或更多次
(匹配最多的可能))
--------------- -------------------------------------------------- ---------------
foo'foo'
----------- -------------------------------------------------- -------
)结束前瞻
----------------------------- -------------------------------------------------- -
^字符串的开头
----------------------------------- ---------------------------------------------
(组并捕获到\1:
-------------------------------------- ------------------------------------------
\w + word字符(az,AZ,0-9,_)(1或
多次(匹配最多的金额
可能))
------------- -------------------------------------------------- -----------------
)\1结尾
------------------- -------------------------------------------------- -----------
$在可选的\\\
之前,结束
字符串



其他正则表达式



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

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






编辑



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

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

新测试



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



新说明



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


I need a regex that will match blahfooblah but not blahfoobarblah

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

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.

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

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

解决方案

Try:

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

Tests:

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

Regular expression explanation

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

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+)$/

New tests

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

New explanation

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

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

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