Capture a dot with postgres regexp

后端 未结 1 1065
小蘑菇
小蘑菇 2021-01-23 22:24

I have these strings :

3           FD160497.   2016  abcd
3           FD160497   2016  abcd

I want to capture \"FD\", the digits, then the do

相关标签:
1条回答
  • 2021-01-23 23:28

    The first lazy pattern made all quantifiers in the current branch lazy, so your pattern became equivalent to

    .*?(FD)([0-9]{6})(\.)??.*?
                         ^^  ^
    

    See its demo at regex101.com

    See the 9.7.3.1. Regular Expression Details excerpt:

    ...matching is done in such a way that the branch, or whole RE, matches the longest or shortest possible substring as a whole. Once the length of the entire match is determined, the part of it that matches any particular subexpression is determined on the basis of the greediness attribute of that subexpression, with subexpressions starting earlier in the RE taking priority over ones starting later.

    You need to use quantifiers consistently within one branch:

    regexp_matches(string, '.*(FD)([0-9]{6})(\.)?.*') as sqn
    

    or

    regexp_matches(string, '.*[[:blank:]](FD)([0-9]{6})(\.)?.*') as sqn
    

    See the regex demo

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