Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression gone wrong

Tags:

regex

php

I want to find all strings looking like [!plugin=tesplugin arg=dfd arg=2!] and put them in array. Important feature: the string could contain arg=uments or NOT(in some cases). and of course there could be any number of arg's. So the string could look like: [!plugin=myname!] or [!plugin=whatever1 arg=22!] or even [!plugin=gal-one arg=1 arg=text arg=tx99!]. I need to put them all in $strarray items

Here is what i did...

$inp = "[!plugin=tesplugin arg=dfd!] sometxt [!plugin=second arg=1 arg=2!] 1sd";
preg_match_all('/\[!plugin=[a-z0-9 -_=]*!]/i', $inp, $str);

but $str[0][0] contains:

[!plugin=tesplugin arg=dfd!] sometxt [!plugin=second arg=1 arg=2!]

instead of putting each expression in a new array item.. I think my problem in regex.. but can't find one. Plz help...

like image 934
Andrew Bro Avatar asked Mar 14 '26 07:03

Andrew Bro


1 Answers

The last ] needs to be escaped and the - in the character class needs to be at the start, end, or escaped. As is it is a range of ascii characters between a space and underscore.

\[!plugin=[a-z0-9 \-_=]*!\]

Regex101 Demo: https://regex101.com/r/zV4bO2/1

like image 166
chris85 Avatar answered Mar 15 '26 20:03

chris85



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!