PHP正则表达式提取键值逗号分隔
问题描述:
I want to extract some from a string that KEYs are separated from VALUEs by colon(:) and s separated by comma(,). The problem is that VALUEs can contain comma. As an example:
category:information technology, computer,publisher:Elsevier (EV),subject:Ecology, Evolution, Behavior and Systematics
In this example the KEYs that must extract are: category, publisher and subject. The final result must be as follow:
category = information technology, computer
publisher = Elsevier (EV)
subject = Ecology, Evolution, Behavior and Systematics
I tried to write a recursive regex but it doesn't work :
(category|publisher|subject):(.*?)(?:,(?R)|.?)
Can someone help to solve this problem. Thanks.
我想从一个字符串中提取一些字符串,这些字符串通过冒号(:)和以逗号分隔的s与VALUE分开 (,)。 问题是VALUE可以包含逗号。 作为示例: p>
类别:信息技术,计算机,出版商:Elsevier(EV),主题:生态,进化,行为和 Systematics
code> pre>
在此示例中,必须提取的KEY包括:category,publisher和subject。
最终结果必须如下: p>
\ n
category =信息技术,computer
publisher = Elsevier(EV)
subject =生态,进化,行为和系统学 pre>
我试过 写一个递归正则表达式,但它不起作用: p>
(category | publisher | subject):(。*?)(?:,(?R)|。?)
code> pre>
有人可以帮助解决这个问题。 谢谢。 p>
div>
答
Well, if you can add a comma to the end of the string, I think this works:
(\w+):([^:]+),
Edit:
Jonathan Kuhn is totally right:
(\w+):([^:]+)(?:,|$)
This works