Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex with numbers in curly brackets

Tags:

regex

I got this regex:

^\+[0-9]{12,12}$

Can anyone explain the meaning of the two values between the curly brackets {12,12}?

like image 422
Sarika Avatar asked Dec 06 '25 04:12

Sarika


1 Answers

{12,12} is a quantifier (see Limiting Repetition section) telling the regex engine that the preceding subpattern should repeat minimum 12 and maximum 12 times. In ^+[0-9]{12,12}$ it means that a digit within 0-9 range should be repeated exactly 12 times. The string should start with a plus, and only contain the plus + 12 digits.

The syntax is {min,max}, where min is zero or a positive integer number indicating the minimum number of matches, and max is an integer equal to or greater than min indicating the maximum number of matches. If the comma is present but max is omitted, the maximum number of matches is infinite. So {0,1} is the same as ?, {0,} is the same as *, and {1,} is the same as +. Omitting both the comma and max tells the engine to repeat the token exactly min times.

Thus, we just can use {12} (it is equal to {12,12}):

^\+[0-9]{12}$

See demo at regex101.com

like image 86
Wiktor Stribiżew Avatar answered Dec 07 '25 17:12

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!