繁体   English   中英

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

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

我需要匹配一个模式not check一个更大的字符串。

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

但在那之后它不应该有一个数字。 所以, not check <some words>应该匹配但not check 67 <some words>不应该匹配。 我试过这些:

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)

它不起作用。 这总是返回一个匹配。

\D* in not\s*check\s*\D*表示匹配到第一个数字如果前面没有数字,则不匹配

利用

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

证明

解释

--------------------------------------------------------------------------------
  \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