我有这些字符串:
3 FD160497. 2016 abcd
3 FD160497 2016 abcd
我想捕获“FD”、数字,然后是点(如果存在)。
我试过这个:
SELECT
sqn[1] AS letters,
sqn[2] AS digits,
sqn[3] AS dot
FROM (
SELECT
regexp_matches(string, '.*?(FD)([0-9]{6})(\.)?.*') as sqn
FROM
mytable
) t;
(PostgreSQL 9.5.3)
“点”列在这两种情况下都是 NULL
,我真的不知道为什么。
它适用于 regex101 .
最佳答案
第一个懒惰模式使当前分支中的所有量词都懒惰,所以你的模式等价于
.*?(FD)([0-9]{6})(\.)??.*?
^^ ^
参见 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.
您需要在一个分支中始终如一地使用量词:
regexp_matches(string, '.*(FD)([0-9]{6})(\.)?.*') as sqn
或
regexp_matches(string, '.*[[:blank:]](FD)([0-9]{6})(\.)?.*') as sqn
参见 regex demo
关于regex - 使用 postgres 正则表达式捕获一个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41169426/