Postgresql regexp_matches syntax not working as expected

前端 未结 1 579
庸人自扰
庸人自扰 2021-01-28 06:38

I use the Postgres regexp_matches function to extract numbers.

The regular expression I use is

4([\\s\\-\\/\\.]*?0){3}([\\s\\-\\/\\.]*?[         


        
1条回答
  •  醉梦人生
    2021-01-28 07:38

    The regexp_matches(string text, pattern text [, flags text]) function returns the captured values:

    Return all captured substrings resulting from matching a POSIX regular expression against the string.

    You may fix the expression using non-capturing groups:

    SELECT unnest(regexp_matches('4-0001-1234 4.0001.12344  4-0-0-0-1-1234', '4(?:[\s/.-]*0){3}(?:[\s/.-]*[12])(?:[\s/.-]*\d){4}', 'g'));
    

    See the online demo.

    BTW, you do not need to escape - when it is at the start/end of the bracket expression, and there is no need to escape neither / nor . there. I also suggest removing {1} as a = a{1} in any regex supporting limiting quantifiers.

    0 讨论(0)
提交回复
热议问题