简体   繁体   English

Python 正则表达式匹配以大写字母和长度范围开头的字母数字

[英]Python regex to match alphanumeric starting with capital letter and length range

I need to match strings like these:我需要匹配这样的字符串:

NEV2297075
WB/03/001/585269
WB/03/HK/585065

It should start with 2 or more capital letters and end with a number, can contain AZ,/ and numbers in between and total length should be between 10 and 22它应该以 2 个或多个大写字母开头并以数字结尾,可以包含 AZ,/ 和介于两者之间的数字,总长度应在 10 到 22 之间

For this I used:为此,我使用了:

re.findall(r'[A-Z]{2,}[A-Z\/0-9]{1,}[0-9]{10,22}'

I see that the length range is applying to the expression immediately preceding it.我看到长度范围正在应用于它之前的表达式。 How to apply length range to entire string?如何将长度范围应用于整个字符串?

The quantifier here [0-9]{10,22} repeats matching 10 - 22 digits.此处的量词[0-9]{10,22}重复匹配 10 - 22 位数字。

If you want to verify the number of total characters, you should anchor the string and verify the number of characters until the next anchor using a lookahead ^(?=[AZ\d,/]{10,22}$)如果要验证总字符数,应锚定字符串并使用前瞻验证直到下一个锚点的字符数^(?=[AZ\d,/]{10,22}$)

^(?=[A-Z\d,/]{10,22}$)[A-Z]{2,}[A-Z\d,/]*\d$

Regex demo正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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