Skip to content

RegExp for an identifier

I am trying to write a regular expression for an ID which comes in the following formats:




  • a – optional 0-3 digits followed by an underscore if there’s at least
    a digit (if there is underscore is required)
  • b – 8 alphanumeric characters
  • c – 4 alphanumeric characters
  • d – 4 alphanumeric characters
  • e – 4 alphanumeric characters
  • f – 12 alphanumeric characters

I have came up with this regexp but I would appreciate any guidance and/or corrections. I am also not too sure how to handle the optional underscore in the first segment if there are no digits up front.




Your regex looks good. To optionally match the first 3 digits with an underscore, you can wrap that group with ()?. Also you can force the presence of a digit before the underscore by using {1,3} instead of {0,3}.

Unless you expect that multiple identifiers are following each other without space and should be matched as one, you can drop the last + (for multiple matches on the same line, you already have the g option).

The final regex is ([a-zA-Z0-9]{1,3}_)?[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}

See here for a complete example.

If you also do not need to capture the individual 4-alphanumeric groups, you can simplify your regex into:


See here for an example.

1 People found this is helpful