phpregexplaceholdertext-parsingdelimited

Match curly braced placeholders with a variable number of dot-delimited internal values


I have strings like : {$foo.bar} and {$foo.bar.anything}

WHERE : foo AND bar AND anything === alphanumeric

i want to match the above 2 strings in PHP via preg_match(regular expression) except those without any dot for example : {$foo}


Solution

  • /{\$[\da-z]+(?:\.[\da-z]+)+}/i
    

    matches

    {$foo.bar}
    {$foo.Bar.anything}
    {$foo.bar.anything1.anything2.anything3}
    {$foo.bar.anything.a.b.c}
    

    does not match

    {$foo}
    {$foo.}
    {$foo bar}
    {$foo.bar anything}
    {$foo.bar......anything..}
    {$foo.bar.anything.}
    {$foo.bar.anything.a.b.c..}
    

    Adopted Joe’s PCRE case-insensitive modifier to shorten it a bit.

    Special thanks to sln for keeping me on my toes until it’s perfect. :)