简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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