Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with simple regular expression

Tags:

regex

php

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}

Your help will be much appreciated, thanks.

like image 364
Ranbir Kapoor Avatar asked Dec 06 '22 19:12

Ranbir Kapoor


1 Answers

/{\$[\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. :)

like image 183
Herbert Avatar answered Dec 10 '22 12:12

Herbert