Intrepid
Intrepid

Reputation: 2831

Regular expression to extract data type and length from string

I have got the following regular expression and I want to extract the data type and length into groups so that I can act on them.

^(an|a|n)\..([0-9]*)|(an|a|n)([0-9]*)

The following data types are all valid:

Only 'an' 'n' and 'n' are the valid data types and all can have a '..' range indicator between the type and the number (or not).

The issue I am having is the regular expression doesn't work when I process 'ang..35' since it's matching 'an' in the text, but I want this to be invalid if it cannot find any of the valid data types.

Upvotes: 0

Views: 173

Answers (1)

Bergi
Bergi

Reputation: 665121

when I process 'ang..35' it's matching 'an' in the text but I want it to be invalid

Then you also want to use an $ end-of-string anchor.

For example:

/^(a?n)((?:\.\.)?\d+)$/

Upvotes: 1

Related Questions