Simple regex problem

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

WHERE: foo AND bar AND anything === alphanumeric

I want to combine 2 lines in PHP via preg_match(regular expression) , except for those who do not have a point for example : {$foo}

Your help will be greatly appreciated, thanks.

+4
source share
8 answers
 /{\$[\da-z]+(?:\.[\da-z]+)+}/i 

coincidences

 {$foo.bar} {$foo.Bar.anything} {$foo.bar.anything1.anything2.anything3} {$foo.bar.anything.abc} 

does not match

 {$foo} {$foo.} {$foo bar} {$foo.bar anything} {$foo.bar......anything..} {$foo.bar.anything.} {$foo.bar.anything.abc.} 

Accepted by Joe & rsquo; using the case insensitive PCRE method to shorten it a bit.

Special thanks to sln for keeping me on his feet until he becomes perfect. :)

+3
source

Assuming php regex matches perl

 ^\w+\.[\.\w]+$ 

This means that it starts with one or more alphanumeric characters, followed by . followed by a few alphanumeric characters or . . $ means all the way to the end of the line.

If it cannot end with a symbol . then

 ^\w+\.[\.\w]+\w$ 

If .. not allowed, it becomes harder because ell regex mechanisms that indicate repetition of multi char subexpressions are not processed. But if so, I think something like

 ^\w+(\.\w+)+$ 

This means starting with one or more alphanumeric characters, after one or more repetitions on . followed by one or more alphanumeric characters. $ means all the way to the end of the line.

+2
source

You probably want preg_match_all , not preg_match - it gets all the matches, as the name suggests, not just the first.

Regarding the required regex, something like this should work

 /\{\$[a-z0-9]+\.([a-z0-9\.]+)+\}/i 
+2
source

If you look at a Regular expression that matches a string that doesn't contain a word? it looks like you could use ^ ((?!..).) * $ to not include it. Use \ to exit the point.

+2
source
 /(\{\$[az]+\.([az][az.])*[az]+\})/ 

So, you first match foo and the point {$foo. , then arbitrarily any characters and periods {$foo.bar. and finally another line of characters. {$foo.bar.anything}

+2
source
 \{\$[A-Za-z0-9]+\.[A-Za-z0-9]+\.?[A-Za-z0-9]*\} 
+2
source
 \{\$[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+\} 

First match {$ . Then match any alphanumeric string. Then match any alphanumeric strings starting with . . Then match } .

+2
source

This is my solution to the problem, with some alternatives depending on what you definitely want to extract.

  • Retrieves only the whole thing {$aaa.bbb[.ccc[.ddd ...]]} provided that it contains at least one point
  • Retrieves content from the {$aaa.bbb} object (eg. aaa.bbb )
  • Only consider tags composed by two or three components (ignore {$aaa} or {$aaa.bbb.ccc.ddd} ).

code:

 <?php $subject = '{$foo.bar} {$foo.bar.baz} {$foo} {$another-foo.bar} {$foo.bar.baz.boh}'; print "Matching the whole string\n"; preg_match_all( '/{\$[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)+}/', $subject, $m); print var_export($m) ."\n\n"; print "Matching only the content\n"; preg_match_all( '/{\$([a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)+)}/', $subject, $m); print var_export($m) ."\n\n"; print "Matching for strings containing only 1 or two dots\n"; preg_match_all( '/{\$([a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+){1,2})}/', $subject, $m); print var_export($m) ."\n\n"; 
+2
source

All Articles