有人可以用一个正则表达式来帮助我验证一个字符串是否在249-268范围内,并且如果可能的话解释一下它是如何在正则表达式的每个块中运行的?
由于
答案 0 :(得分:1)
要匹配您可以使用的整个字符串:
^2(49|5[0-9]|6[0-8])$
请参阅演示:https://regex101.com/r/rJ2lH5/1
(如果您不想匹配整个字符串,可以删除^
和$
。
现在,正则表达式真的不知道这是否是“范围匹配”,它只是匹配我们告诉它的数字。在这种特定情况下,模式是:
^ # assert position at start of a line
2 # matches the character 2 literally
1st Alternative: 49
49 # matches the characters 49 literally
2nd Alternative: 5[0-9]
5 # matches the character 5 literally
[0-9] # match a single character in the range between 0 and 9
3rd Alternative: 6[0-8]
6 # matches the character 6 literally
[0-8] # match a single character in the range between 0 and 8
$ # assert position at end of a line