Skip to content
Advertisement

RegExp for an identifier

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

7_b4718152-d9ed-4724-b3fe-e8dc9f12458a

b4718152-d9ed-4724-b3fe-e8dc9f12458a

[a_][b]-[c]-[d]-[e]-[f]

  • 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.

/([a-zA-Z0-9]{0,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})+/g

Advertisement

Answer

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:

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

See here for an example.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement