Matching a group that may or may not exist Matching a group that may or may not exist oracle oracle

Matching a group that may or may not exist


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.


To match a character (or in your case group) that may or may not exist, you need to use ? after the character/subpattern/class in question. I'm answering now because RegEx is complicated and should be explained: only posting the fix without the answer isn't enough!

A question mark matches zero or one of the preceding character, class, or subpattern. Think of this as "the preceding item is optional". For example, colou?r matches both color and colour because the "u" is optional.

Above quote from http://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm


Try this:

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