0

I want create a regex with an optimized way that will check 2 characters range from A-Z,a-z and 3 digits, I tried with below regex

^[A-Za-z]{2}\d{3}$

but i am not sure the regex is optimized or not ?

Thanks in advance...

laddu
  • 1
  • 2
  • 2
    It is the minimal (and optimal) regexp for your requirement. You should not worry about "optimality" of regular expressions in most cases; the main point to be careful about is repeating the repeats (read up on "catastrophic backtracking" if really interested). Note that it not only match `US202`, but also `PLATYPUS2020 FOR MAYOR`. If you want to avoid this, you need to use the start/end anchors (`^`, `$`). – Amadan Mar 09 '20 at 06:14
  • @Amadan sorry i forgot put start and end of the regex.. but my requirement is to the regex which check the result in milliseconds,for that using this can i achieve? – laddu Mar 09 '20 at 06:18
  • Depending on how much data you have to process, don't worry, the difference is generaly less than `1%` for `10000` values and `\d` better than `[0-9]` for `1 000 000` values – Toto Mar 09 '20 at 10:14

1 Answers1

0

Your regex is optimal. May one thing to do could be replace \d with [0-9]. your could read this for info. \d matches not only 0-9, it matches all digit, also arabic, persian, etc. Though is less performant. If you want to figure out how good is you regex eprforming use this website. You could roughly estimate what would be the performance by viewing the "steps" and also debugging the regex. But your example is extreme simple, performance is not a question you should bother is such example. In the most cases of regex you should not bother at all. There could be thoughts about this, but you would need a comlex set of data with complex pattern requeriments, to this be meaninful. A good rule of thumb for a performant regex is - be as specific as you can, which you regex is, if you have no more info about your dataset.

BitBeats
  • 886
  • 8
  • 23