Reputation: 47
I got this regex:
^\+[0-9]{12,12}$
Can anyone explain the meaning of the two values between the curly brackets {12,12}
?
Upvotes: 3
Views: 2825
Reputation: 626961
{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}
, wheremin
is zero or a positive integer number indicating the minimum number of matches, andmax
is an integer equal to or greater thanmin
indicating the maximum number of matches. If the comma is present butmax
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 exactlymin
times.
Thus, we just can use {12}
(it is equal to {12,12}
):
^\+[0-9]{12}$
Upvotes: 10