Code: Select all
$regex = '/[^\[\]\s]++|(?=\[\])/';
$str = 'foobar2f[f1][f][]';
preg_match_all($regex, $str, $matches);
print_r($matches[0]);Code: Select all
Array
(
[0] => foobar2f
[1] => f1
[2] => f
[3] =>
)Moderator: General Moderators
Code: Select all
$regex = '/[^\[\]\s]++|(?=\[\])/';
$str = 'foobar2f[f1][f][]';
preg_match_all($regex, $str, $matches);
print_r($matches[0]);Code: Select all
Array
(
[0] => foobar2f
[1] => f1
[2] => f
[3] =>
)GeertDD wrote:If it is, I think you're needlessly complicating that regex.stereofrog wrote:Is this what you're looking for?Code: Select all
$re = '~\w+(?=\[)|(?<=\[)\w+(?=\])~';
Try this pattern:Code: Select all
/[^\[\]]+/
Jcart isn't clear about that. This would work if you don't want to match 'XYZ':stereofrog wrote:Given a string like this "foo[bar] XYZ qux[quux]", should the expression also match "XYZ"? I was under the impression that Jcart only needs the words between "[ ]" or immediately before "[". Your expression apparently won't work for this case.
Code: Select all
/[^\[\]\s]++(?=\[)|(?=\[\])/