用于检测 cuits 的正则表达式 - Python
Regex to detect cuits - Python
我需要找到一个正则表达式来识别以下序列。两个数space8个数space一个数。数字之间可以出现多个字符组合。
\d{2}.?\d{8}.?\d{1}
应该匹配:
11 11111111 1
11-11111111-1
11.11111111.1
11|11111111|1
11.11111111 1
11 11111111 1
不应该匹配:
11 11111111 11
1 11111111 11
使用
(?<!\d)\d{2}\D*\d{8}\D*\d(?!\d)
参见proof。
解释
--------------------------------------------------------------------------------
(?<! 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
我需要找到一个正则表达式来识别以下序列。两个数space8个数space一个数。数字之间可以出现多个字符组合。
\d{2}.?\d{8}.?\d{1}
应该匹配:
11 11111111 1
11-11111111-1
11.11111111.1
11|11111111|1
11.11111111 1
11 11111111 1
不应该匹配:
11 11111111 11
1 11111111 11
使用
(?<!\d)\d{2}\D*\d{8}\D*\d(?!\d)
参见proof。
解释
--------------------------------------------------------------------------------
(?<! 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