簡體   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