Regex to detect cuits - Python

Viewed 29

I need to find a regex that allows me to identify the following sequence. Two numbers space 8 numbers space one number. Between the numbers can appear multiple combinations of characters.

\d{2}.?\d{8}.?\d{1}

Should match:

11 11111111 1
11-11111111-1
11.11111111.1
11|11111111|1
11.11111111  1
11  11111111  1

Should not match:

11  11111111  11
1  11111111  11
1 Answers

Use

(?<!\d)\d{2}\D*\d{8}\D*\d(?!\d)

See proof.

EXPLANATION

--------------------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  \d{2}                    digits (0-9) (2 times)
--------------------------------------------------------------------------------
  \D*                      non-digits (all but 0-9) (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  \d{8}                    digits (0-9) (8 times)
--------------------------------------------------------------------------------
  \D*                      non-digits (all but 0-9) (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  \d                       digits (0-9)
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
  )                        end of look-ahead
Related