[英]Matching words that may contain 1-2 digits
I use the following regex for matching words with a length of 4 that has 1 number and 3 capital letters: 我使用以下正则表达式来匹配长度为4且具有1个数字和3个大写字母的单词:
\b(?=[A-Z]*\d[A-Z]*\b)[A-Z\d]{4}\b
What I would like to know is how I need to modify the expression to filter out words with a length of 10, that contains 0-2 numbers. 我想知道的是我需要修改表达式来过滤掉长度为10的单词,其中包含0-2个数字。
\b(?=[A-Z]*\d[A-Z]*\b)[A-Z\d]{10}\b
This will work for 1 number occurence, but how do i extend it to filter 0 and 2 numbers as well? 这将适用于1个数字出现,但我如何将其扩展为过滤0和2数字呢?
Sample: http://regexr.com?32u40 样本: http : //regexr.com?32u40
Put the length check into the lookahead: 将长度检查放入前瞻:
\b(?=[A-Z\d]{10}\b)(?:[A-Z]*\d){0,2}[A-Z]*\b
Explanation: 说明:
\b # Start at a word boundary
(?= # Assert that...
[A-Z\d]{10} # 10 A-Z/digits follow
\b # until the next word boundary.
) # (End of lookahead)
(?: # Match...
[A-Z]* # Any number of ASCII uppercase letters
\d # and exactly one digit
){0,2} # repeat 0, 1 or 2 times.
[A-Z]* # Match any number of letters
\b # until the next word boundary.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.