Fernando.M
Fernando.M

Reputation: 137

RegEx for N digits always starting with 2

I want to validate these rules:

1)Only numbers

2)Must have 13 digits

3)Always start with number 2

4)May have dots after the first 8 digits, 2 digits and before last digit like:

(XXXXXXXX.XX.XX.X)

Example:

2437313600001 - 23610579.00.03.1

So far I have this

^([0-9]-?){13}$

How do I solve this problem?

Upvotes: 1

Views: 573

Answers (1)

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

Reputation: 18357

You can use this regex,

^2\d{7}(?:\.?\d){5}$

Explanation:

  • ^ - Start of string
  • 2 - Start first character with 2 only
  • \d{7} - Next seven characters can be any digits
  • (?:\.?\d){5} - Next five characters can be any digits but they can be preceded by an optional dot before them
  • $ - End of string

Regex Demo

Upvotes: 1

Related Questions