Matching a group that may or may not exist

前端 未结 5 2635
迷失自我
迷失自我 2021-02-20 03:03

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

BLOOKKOKATU 20 A 773 00810 HELSINKI SUOMI
-------------------- ----- -------- -----
          1                 


        
5条回答
  •  梦如初夏
    2021-02-20 03:39

    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.

提交回复
热议问题