Sarika
Sarika

Reputation: 47

Regex with numbers in curly brackets

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

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

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}, 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

Upvotes: 10

Related Questions