正则表达式找到UUID和文本

时间:2017-03-16 05:28:42

标签: sql regex postgresql

我有一个看起来像这样的字符串:

"name=Guy&Id=e8679b5a-bf27-46c6-aaf0-ef0eced1945e&Family.Id=e8679b5a-bf27-46c6-aaf0-ef0eced1945e,aaa79b5a-bf27-46c6-aaf0-ef0eced1945e,eee79b5a-bf27-46c6-aaf0-ef0eced1945e&Children.Id=bbb79b5a-bf27-46c6-aaf0-ef0eced1945e,ccc79b5a-bf27-46c6-aaf0-ef0eced1945e,ddd79b5a-bf27-46c6-aaf0-ef0eced1945e&Father.Id=fff79b5a-bf27-46c6-aaf0-ef0eced1945e"

我想有一个正则表达式,可以找到所有组合(String.Id和它们的UUID),如:

Family | e8679b5a-bf27-46c6-aaf0-ef0eced1945e,aaa79b5a-bf27-46c6-aaf0-ef0eced1945e,vvv79b5a-bf27-46c6-aaf0-ef0eced1945e
Children | bbb79b5a-bf27-46c6-aaf0-ef0eced1945e,ccc79b5a-bf27-46c6-aaf0- ef0eced1945e,ddd79b5a-bf27-46c6-aaf0-ef0eced1945e
Father | fff79b5a-bf27-46c6-aaf0-ef0eced1945e

我试图在PostgreSQL中解析它,目前我可以解析其中一些,但不是全部:

SELECT
regexp_matches('"name=Guy&Id=e8679b5a-bf27-46c6-aaf0-ef0eced1945e&Family.Id=e8679b5a-bf27-46c6-aaf0-ef0eced1945e,aaa79b5a-bf27-46c6-aaf0-ef0eced1945e,vvv79b5a-bf27-46c6-aaf0-ef0eced1945e&Children.Id=bbb79b5a-bf27-46c6-aaf0-ef0eced1945e,ccc79b5a-bf27-46c6-aaf0-ef0eced1945e,ddd79b5a-bf27-46c6-aaf0-ef0eced1945e&Father.Id=fff79b5a-bf27-46c6-aaf0-ef0eced1945e"' 
,'&(\w*)\.Id=([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})', 'g')::text

由于

1 个答案:

答案 0 :(得分:1)

您可以使用regex like so

(?:name=[^&]+&Id=[^&]+)?&([a-zA-Z]+).Id=([^&]+)

取代:

$1 | $2\n

它的作用:

首先,它会查找可选的name=[^&]+&Id=[^&]+name=,后跟至少一个非&个字符([^&]+),然后是{{1 },后跟至少一个非&Id=字符(&)。这全部包含在非捕获组[^&]+中,后跟(?: ... )以表示该组是可选的。

正则表达式的其余部分查找?,然后是第一个包含至少一个字母字符(&)的捕获组($1),后跟{{1 },然后是至少一个非([a-zA-Z]+)字符(.Id=)的第二个捕获组($2)。