1

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?

1 Answer 1

2

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+)\)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.