I'm trying to write a regular expression for parsing address.
My address strings have next format: AddressLine (City) SomeFixedText
The only problem that (City)
part is optional. For example:
Abingdon Road (Oxford) Water Network Pumping
Adderbury Water Network Pumping
Without capturing optional group I've come up with following expression:
(?<Line>.*) \((?<City>.*)\) Water Network Pumping
But of course it doesn't not handle the second case. I tried to use an optional group:
(?<Line>.*)( \((?<City>.*)\))? Water Network Pumping
But in this case Line and City are captured incorrectly for the first case.
Here is the fiddle: https://dotnetfiddle.net/crHlta
How can I handle this situation?