Highlighting a PHP Regex Value Key for a Comma

I want to extract from the string that KEYs are separated from VALUE by a colon (:) and s are separated by a comma (,). The problem is that VALUE may contain a comma. As an example:

category:information technology, computer,publisher:Elsevier (EV),subject:Ecology, Evolution, Behavior and Systematics

In this example, the KEYS to be extracted are: category, publisher, and subject. The end result should be as follows:

category = information technology, computer
publisher = Elsevier (EV)
subject = Ecology, Evolution, Behavior and Systematics

I tried to write a recursive regular expression, but it does not work:

(category|publisher|subject):(.*?)(?:,(?R)|.?)

Can someone help solve this problem. Thanks.

+4
source share
1 answer

Well, if you can add a comma to the end of the line, I think this works:

(\w+):([^:]+),

Edit:

Jonathan Coon is absolutely right:

(\w+):([^:]+)(?:,|$)

It works

+5
source

Source: https://habr.com/ru/post/1629477/


All Articles