Home > other >  Improving a regex syntax [closed]
Improving a regex syntax [closed]

Time:10-09

I have the next regex and I was thinking if there was the possibility to improve it since I have little knowledge about it.

/([A-Z]){1,3}\.\d\d?\.\d\d?\.?\d?/g

The main function of the regex is to match:

  • 1 or more uppercase letters
  • .
  • 1 or more digits
  • .
  • 1 or more digits
  • . (optional)
  • 1 or more digits (optional)

Match examples: A.1.1, AA.1.1, AE.5.10, Z.10.10.1, BV.1.6.10

Is it possible to improve my regex?.

CodePudding user response:

You may try something like this

[A-Z] (?:\.\d ){2,3}
[A-Z]      // one or more uppercase letters
(?:\.\d )  // one or more digits preceded by a dot, in a non-capturing group
{2,3}      // repeats the previous rule for 2 to 3 times

check the proof

  • Related