简体   繁体   English

如何匹配/搜索特定模式但没有另一个特定模式?

[英]How to match/search for a specific pattern but not having another specific pattern?

I need to match for a pattern not check in a bigger string.我需要匹配一个模式not check一个更大的字符串。

bigger_string = '<some words and> do not check <some other words>'

But it shouldn't have a number after that.但在那之后它不应该有一个数字。 So, not check <some words> should match but not check 67 <some words> shouldn't match.所以, not check <some words>应该匹配但not check 67 <some words>不应该匹配。 I tried these:我试过这些:

re.findall(re.compile(r'not\s*check\s*\D*', re.I), bigger_string)

re.findall(re.compile(r'not\s*check\s*[^0-9]*', re.I), bigger_string)

It doesn't work.它不起作用。 This always returns a match.这总是返回一个匹配。

\D* in not\s*check\s*\D* means match up to the first digit , not match ony if there is no digit ahead . \D* in not\s*check\s*\D*表示匹配到第一个数字如果前面没有数字,则不匹配

Use利用

\bnot\s+check\b(?!\s*\d)\D*

See proof .证明

EXPLANATION解释

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  not                      'not'
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  check                    'check'
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  \D*                      non-digits (all but 0-9) (0 or more times
                           (matching the most amount possible))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM