Nate Nate - 1 year ago 74
PHP Question

regex php match "{loop var=" \w_[] "}"

I have a string

{loop var=someText}

where someText can be: \w or _ or [ or ]

i have this:

/[{loop var=][\w_\[\]][}]/
/\b{loop var=\b[\w_\[\]]\b}\b/

needs to match entire string of any of below

{loop var=MainMenu}
{loop var=menu[0]}
{loop var=Menu_main}


Answer Source

You may use

/{loop var=[][\w]+}/

See the regex demo

When you enclosed the [{loop var=] and [\w_\[\]] within non-quantified character classes, you made the subpatterns that match 1 char, while you need to match character sequences.


  • {loop var= - a literal character sequence
  • [][\w]+ - one or more (+) chars from the character set - word chars (incl. _), ] and[`
  • } - a literal }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download