RyosanCiffer
RyosanCiffer

Reputation: 115

Parse text with python RegEx groups with group order changing

I want to parse places names like this:

à : Paris (France)

But often it encounters a place in USA like this

à : Boston (MA) (États-Unis)

I tried this to parse it:

place='à :  (?P<city>.+) (\((?P<country>.+)\)|(\((?P<state>.+)\) \((?P<country>.+)\)))'

But it doesn't seem to work and it goes beyond my current understanding of beginner.

How to handle this?

Upvotes: 1

Views: 33

Answers (1)

akuiper
akuiper

Reputation: 214957

You can make the state group optional using the quantifier ?:

à : (?P<city>\S+) (?:\((?P<state>\S+)\) )?\((?P<country>\S+)\)
#                 ^^^                   ^^

See the demo.

In which case it will match both:

à : (?P<city>\S+) \((?P<state>\S+)\) \((?P<country>\S+)\)

and

à : (?P<city>\S+) \((?P<country>\S+)\)

Upvotes: 2

Related Questions