Regex to match text between commas

Pr your answer to my question, here is a regexp to match a string that occurs between two commas.

(?<=,)[^,]+(?=,)

This regexp does not match, and hence do not consume, the delimiting commas.
This regexp would match ” and hence do not consume” in the previous sentence.

The fact that your regexp matched and consumed the commas was the reason why your attempted regexp only matched every other candidate.

Also if the whole input is a single string you will want to prevent linebreaks. In that case you will want to use;

(?<=,)[^,\n]+(?=,)

http://www.phpliveregex.com/p/1DJ

Leave a Comment