0

这个:

select regexp_matches('test text user:testuser,anotheruser hashtag:peach,phone,milk site:youtube.com,twitter.com flair:bobby', '^.*?(?=\s+[^:\s]+:)|([^:\s]+):([^:\s]+)','gi');

只给我一个组匹配和一个 NULL 行:

regexp_matches  
-----------------
 {NULL,NULL}
 {flair,bobby}

当我在这里测试它时它工作正常:

https://regex101.com/r/AxsatL/3

我究竟做错了什么?

4

1 回答 1

0

您可以使用

'^(?:(?!\s+[^:\s]+:).)*|[^:\s]+:[^:\s]+'

这里的重点是保持所有量词贪婪并删除所有捕获括号。

^(?:(?!\s+[^:\s]+:).)*部分将匹配 - 从字符串的开头 - 任何字符,0 次或多次出现,不开始以下模式的序列:1+ 空格,1+ 字符而不是:和空格,然后是:.

在线测试

select regexp_matches(
    'test text user:testuser,anotheruser hashtag:peach,phone,milk site:youtube.com,twitter.com flair:bobby',
    '^(?:(?!\s+[^:\s]+:).)*|[^:\s]+:[^:\s]+',
    'gi'
);

结果:

在此处输入图像描述

于 2019-10-19T22:11:25.993 回答