This regex should match lists just like in Markdown:
/((?:(?:(?:^[\+\*\-] )(?:[^
]+))(?:|
?))+)/m
It works in Javascript (with g
flag added) but I have problems porting it to PHP. It does not behave greedy. Here's my example code:
$string = preg_replace_callback('`((?:(?:(?:^\* )(?:[^
]+))(?:|
?))+)`m', array(&$this, 'bullet_list'), $string);
function bullet_list($matches) { var_dump($matches) }
When I feed to it a list of three lines it displays this:
array(2) { [0]=> string(6) "* one " [1]=> string(6) "* one " } array(2) { [0]=> string(6) "* two " [1]=> string(6) "* two " } array(2) { [0]=> string(8) "* three " [1]=> string(8) "* three " }
Apparently var_dump
is being called three times instead of just once as I expect from it since the regex is greedy and must match as many lines as possible. I have tested it on regex101.com.
How do I make it work properly?