Matching a group that may or may not exist

前端 未结 5 2630
迷失自我
迷失自我 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:44

    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

提交回复
热议问题