简体   繁体   English

令牌可以在正则表达式中重复多少次

[英]how many times a token can be repeated in regex

I am a beginner in regex. 我是regex的初学者。 I read that \\b[1-9][0-9]{2,4}\\b matches a number between 100 and 99999 , but what is the difference between \\b[1-9][0-9]{2,4}\\b and \\b[1-9]{2,4}\\b and why mentioned pattern matches a number between 100 and 99999 ? 我读到\\b[1-9][0-9]{2,4}\\b匹配10099999之间的数字,但是\\b[1-9][0-9]{2,4}\\b什么区别\\b[1-9][0-9]{2,4}\\b\\b[1-9]{2,4}\\b ,为什么提到的模式匹配10099999之间的数字?

I think because min=2 and max=4 pattern matches a number between 10 and 9999, because the minimum two-digit number is 10 and the maximum four-digit number is 9999. 我认为因为min=2max=4模式匹配10到9999之间的数字,因为最小的两位数字是10,最大的四位数字是9999。

Your understanding is not entirely correct. 您的理解并不完全正确。

  xy{2,4} matches x followed by 2 to 4 y => xyy or xyyy or xyyyy

In your case [1-9][0-9]{2,4} matches any digit between [1-9] followed by any 2 to 4 digits in [0-9] . 在你的情况[1-9][0-9]{2,4}匹配之间的任何数字[1-9]后跟任意2 to 4中数字[0-9] So it matches any number between 所以它匹配之间的任何数字

100 - 1 coming from [1-9] and 00 coming from [0-9]{2,4}

and

99999 - 9 coming from [1-9] and 9999 coming from [0-9]{2,4}

but what is different between \\b[1-9][0-9]{2,4}\\b and \\b[1-9]{2,4}\\b ...? 但是\\b[1-9][0-9]{2,4}\\b\\b[1-9]{2,4}\\b ...有什么区别?

In the second regex you cant have any 0 s 在第二个正则表达式中,您不能有任何0 s

so 10 is no match in the second one. 所以第二个中没有10。

AND

[1-9][0-9]{2,4} matches 1 00 to 9 9999 (the first part is 1-9 and than you can add 00 - 9999) [1-9][0-9]{2,4}匹配1 00到9 9999(第一部分是1-9,然后您可以加00-9999)

{2,4} belongs only to [0-9] ... so it is exactly one [1-9] and 2-4 [0-9] {2,4}仅属于[0-9] ...,因此恰好是[1-9]和2-4 [0-9]

to try out the regex, you can use https://regex101.com/ 要试用正则表达式,可以使用https://regex101.com/

The expression says: First a blank, then a digit in range 1-9, then two to four digits in range 0-9. 该表达式表示:首先是空格,然后是1-9范围内的数字,然后是2到4范围0-9之间的数字。 Thus, the minimum number is 100. 因此,最小数量为100。

\\b[1-9][0-9]{2,4}\\b ==> \\b[1-9][0-9]{2,4}\\b ==>

\\b ==> ensures word boundary. \\b ==>确保单词边界。 ie., doesn't allow your string to be inside another number string like 111111000011111 即,不允许您的字符串位于另一个数字字符串之内,例如111111000011111

[1-9][0-9]{2,4} ==> a digit 1 to 9 followed by digits (between 0 to 9) whose min length =2 and max length = 4. This matches strings like : 132,10234 but NOT 012 or 11. [1-9][0-9]{2,4} ==>一个数字1到9,然后是一个最小长度= 2且最大长度= 4的数字(介于0到9之间)。该字符串与以下字符串匹配:132, 10234,但不是012或11。

\\b[1-9]{2,4}\\b ==> matches 1 to 9 2 to 4 times. \\b[1-9]{2,4}\\b ==>匹配1到9 2到4次。 ie, 19, 193, 1934 are all valid 即19、193、1934年均有效

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

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