简体   繁体   中英

how many times a token can be repeated in regex

I am a beginner in 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 ?

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.

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] . 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 ...?

In the second regex you cant have any 0 s

so 10 is no match in the second one.

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)

{2,4} belongs only to [0-9] ... so it is exactly one [1-9] and 2-4 [0-9]

to try out the regex, you can use 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. Thus, the minimum number is 100.

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

\\b ==> ensures word boundary. ie., doesn't allow your string to be inside another number string like 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.

\\b[1-9]{2,4}\\b ==> matches 1 to 9 2 to 4 times. ie, 19, 193, 1934 are all valid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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