-4

I have strings from 000001 00000 to 999999.

I'm looking for a regex to capture the 2000 elements, from 000001 to 002000. Maybe the question is too naïve, but after some attemps, I'm not finding the right one...

Could somebody help, please? Thanks!

EDIT: this question has been identified as possible dublicated of this and I have asked to edit the question to explain why it isn't. The aforementioned question is about regex in general but I'm asking for a very specific case (you can argue if the specific case is trivial or not, but that is a different question ;)

fgalan
  • 10,250
  • 6
  • 36
  • 70

2 Answers2

1

You may use this one:

^0*(2000|1?[0-9]{3})$

As per your comment, you also want to exclude "000000". Then use

^(?!000000)0*(2000|1?[0-9]{3})$

["000000", "000001", "999999", "001541", "000015", "002000"].forEach(s=> console.log(s, /^(?!000000)0*(2000|1?[0-9]{3})$/.test(s)))

But most real world sane solutions would just parse the number and compare it to a range. This would be less prone to errors and much easier to check and maintain.

Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
0

Check this: ^00([01]\d{3}|2000)$

Be careful, it also matches 000000, but as you said there's no such string so everything is ok.

CrafterKolyan
  • 955
  • 3
  • 12