-1

How can I write a regex expression that checks a certain numeral at a certain place?

For example, if I want to have regex for only numerals, length 7 - I will write something like

^[0-9]{7}$

But how can I make that the 4th digit is always 1?

Example:

0031000 true

9991999 true

9992999 false 
totok
  • 1,334
  • 7
  • 26
dimmxx
  • 97
  • 1
  • 8

1 Answers1

1

You can use this regex to capture two parts of number, before and after digit one:

^\d{3}(?:1)\d{3}$

Using (?:1) (a non-capturing group) mean you need find digit one but dont want capture this. On the final result you get only ^\d{3} and \d{3}$.

Sergio Cabral
  • 5,588
  • 33
  • 35