LazioTibijczyk
LazioTibijczyk

Reputation: 1957

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]

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

Upvotes: 1

Views: 241

Answers (1)

Simon Doppler
Simon Doppler

Reputation: 2093

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.

Upvotes: 2

Related Questions