PHP:RegEx用于3-9个字母和5-50个数字的字符串 [英] PHP: RegEx for string with 3-9 letters and 5-50 numbers
问题描述
如何在PHP中制作仅接受3-9个字母(大写)和5-50个数字的RegEx?
How can I make a RegEx in PHP that only accepts 3-9 letters (uppercase) and 5-50 numbers?
我不太擅长正则表达式.但这不起作用:
I'm not that good at regular expressions. But this one doesn't work:
/[A-Z]{3,9}[0-9]{5,50}/
例如,它匹配ABC12345
,但不匹配A12345BC
For instance, it matches ABC12345
but not A12345BC
有什么想法吗?
推荐答案
这是经典的密码验证"类型的问题.为此,粗略配方"是先检查每个条件,然后我们进行匹配.
This is a classic "password validation"-type problem. For this, the "rough recipe" is to check each condition with a lookahead, then we match everything.
^(?=(?:[^A-Z]*[A-Z]){3,9}[^A-Z]*$)(?=(?:[^0-9]*[0-9]){5,50}[^0-9]*$)[A-Z0-9]*$
我将在下面说明这一点,但是这里有一个变体,请您找出.
I'll explain this one below, but here's a variation that I'll leave for you to figure out.
^(?=(?:[^A-Z]*[A-Z]){3,9}[0-9]*$)(?=(?:[^0-9]*[0-9]){5,50}[A-Z]*$).*$
让我们逐个看一下正则表达式.
Let's look at the first regex piece by piece.
- 我们将正则表达式锚定在字符串^的头部和字符串$断言的末尾之间,以确保匹配项(如果有)是整个字符串.
- 我们有两种先行方式:一种是大写字母,一种是数字.
- 先行查找后,
[A-Z0-9]*
匹配整个字符串(如果它仅包含大写ASCII字母和数字). (感谢@TimPietzcker指出我在车轮上从星点开始时睡着了.)
- We anchor the regex between the head of string ^ and end of string $ assertions, ensuring that the match (if any) is the whole string.
- We have two lookaheads: one for the capital letters, one for the digits.
- After the lookaheads,
[A-Z0-9]*
matches the whole string (if it consists only of uppercase ASCII letters and digits). (Thanks to @TimPietzcker for pointing out that I was asleep at the wheel for starting out with a dot-star there.)
提前行如何工作?
(?:[^A-Z]*[A-Z]){3,9}[^A-Z]*$)
断言,在当前位置(即字符串的开头),我们可以匹配任意数量的不是大写字母的字符,后跟一个大写字母",匹配3到9次.这样可以确保我们有足够的大写字母.请注意,{3,9}
是贪婪的,因此我们将匹配尽可能多的大写字母.但是我们不希望匹配超过我们希望的范围,因此在表达式用{3,9}
进行量化之后,先行检查是否可以匹配不是大写字母的零个或任意个数"的字符,直到最后的字符串,由锚点$
标记.
The (?:[^A-Z]*[A-Z]){3,9}[^A-Z]*$)
asserts that at the current position, i.e. the beginning of the string, we are able to match "any number of characters that are not capital letters, followed by a single capital letter", 3 to 9 times. This ensures we have enough capital letters. Note that the {3,9}
is greedy, so we will match as many capital letters as possible. But we don't want to match more than we wish to allow, so after the expression quantifies by {3,9}
, the lookahead checks that we can match "zero or any number" of characters that are not a capital letter, until the end of the string, marked by the anchor $
.
第二个超前工作方式相似.
The second lookahead works in similar fashion.
有关此技术的更深入说明,您可能需要仔细阅读此页面的密码验证部分,其中有关正则表达式环顾.
For a more in-depth explanation of this technique, you may want to peruse the password validation section of this page about regex lookarounds.
如果您有兴趣,这里是该技术的逐个令牌说明.
In case you are interested, here is a token-by-token explanation of the technique.
^ the beginning of the string
(?= look ahead to see if there is:
(?: group, but do not capture (between 3 and 9 times)
[^A-Z]* any character except: 'A' to 'Z' (0 or more times)
[A-Z] any character of: 'A' to 'Z'
){3,9} end of grouping
[^A-Z]* any character except: 'A' to 'Z' (0 or more times)
$ before an optional \n, and the end of the string
) end of look-ahead
(?= look ahead to see if there is:
(?: group, but do not capture (between 5 and 50 times)
[^0-9]* any character except: '0' to '9' (0 or more times)
[0-9] any character of: '0' to '9'
){5,50} end of grouping
[^0-9]* any character except: '0' to '9' (0 or more times)
$ before an optional \n, and the end of the string
) end of look-ahead
[A-Z0-9]* any character of: 'A' to 'Z', '0' to '9' (0 or more times)
$ before an optional \n, and the end of the string
这篇关于PHP:RegEx用于3-9个字母和5-50个数字的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!