Alex Rohleder
Alex Rohleder

Reputation: 37

What regex match this pattern?

I'm using PHP preg_match_all to parse patterns like: key1: value1, key2: value2 I reach to this regex ~([\w-_]+)\s?:\s?([^,]*)~i it works as spected and the returned array is [['key1: value1', 'key1', 'value1'], ['key2: value2', 'key2', 'value2']] using the PREG_SET_ORDER arg.

BUT if I want to match text that contains a , so to use the text must scape with a backslash \, is there a way in regex to skip the [^,] breaker if it's \,?

Upvotes: 1

Views: 41

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

Use alternation operator to match escaped comma greedily.

~([\w-_]+)\s?:\s?((?:\\,|[^,])*)~i

DEMO

Upvotes: 4

Related Questions