manty
manty

Reputation: 297

What's wrong with this regex pattern?

I'm trying out one simple regex pattern. But it's behaving strange.

re.findall('ABC\-\d{2}\-\d{3,5}(\[[A-Z]\])?', 'ABC-01-1234[Z],ABC-12-5678')

The output is always:

['[Z]']

whereas I want both the strings i.e. ABC-01-1234[Z],ABC-12-5678 as my matched pattern. [Z] is optional. Don't know why ? is not working correctly.

Upvotes: 2

Views: 31

Answers (1)

Andreas Louv
Andreas Louv

Reputation: 47099

Change your regex to:

re.findall('(ABC\-\d{2}\-\d{3,5}(?:\[[A-Z]\])?)', 'ABC-01-1234[Z],ABC-12-5678')

Group around the whole match (...). And non capturing group around your maybe match (?:...)

Tested in JS (not sure if will work in Python):

'ABC-01-1234[Z],ABC-12-5678'.match(/(ABC\-\d{2}\-\d{3,5}(?:\[[A-Z]\])?)/g); // ["ABC-01-1234[Z]", "ABC-12-5678"]

Upvotes: 3

Related Questions