lovesh
lovesh

Reputation: 5411

what is wrong with this regex

The pattern i am trying to match is some characters(letters+digits+hyphens(-)) then 5 digits and then 9 digits. Each part of the pattern is separated by hyphen(-) The 5-digit and 9-digit parts are optional meaning that one of them might be present while other might not be or may be both are present or may be both are absent. So the patterns can be like this

bla-12-bla-98-bla-12345-123456789     all parts(characters+5-digit+9-digit)
bla-bla-123-12345                     9-digit part absent
blasd-123456789                       5-digit part absent
no-1045-numeric-bla-bla               both numeric parts absent

The regex i came up with is this

.+(\d{5})?-?(\d{9})?$

The reasoning for above goes like this. Since the .+ in the beginning matches all characters,the (\d{5})? says that there might be 0 or 1 instance of the 5-digit part thus making it optional.

For -?, if both numeric parts are not present then there woudnt be any - but if both numeric parts are present then there would be 1 - so again i make it optional.

Similarly for (\d{9})?.

But the above pattern does not match anything. I am using python so both group(1) and group(2) return None.

Whats wrong with the above?

Upvotes: 0

Views: 73

Answers (1)

mel-
mel-

Reputation: 533

The problem is the .+ at the beginning. The Regex interpreter is not able to deduce when .+ is supposed to end. So your pattern does in fact match, but your match groups will always be None as you already observed. If you just use

(\d{5})?\-?(\d{9})?$

instead, it will still match all of your examples, and your match groups will be recognized properly.

Upvotes: 2

Related Questions