Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching a group that may or may not exist

My regex needs to parse an address which looks like this:

BLOOKKOKATU 20 A 773 00810 HELSINKI SUOMI
-------------------- ----- -------- -----
          1            2       3      4*

Groups one, two and three will always exist in an address. Group 4 may not exist. I've written a regex that helps me get the first, second and third part but I would also need the fourth part. Part 4 is the country name and can either be FINLAND or SUOMI. If the fourth part didn't exist in an address the fourth group would be empty. This is my regex so far but the third group captures the country too. Any help?

(.*?)\s(\d{5})\s(.*)$

(I'm going to be using this Oracles REGEXP function)

like image 923
Mridang Agarwalla Avatar asked Jul 12 '11 12:07

Mridang Agarwalla


1 Answers

Change the regex to:

(.*?)\s(\d{5})\s(.+?)\s?(FINLAND|SUOMI)?$

Making group three none greedy will let you match the optional space + country choices. If group 4 doesn't match I think it will be uninitialized rather than blank, that depends on language.

like image 153
NorthGuard Avatar answered Sep 18 '22 13:09

NorthGuard