shane18 Posted September 24, 2009 Share Posted September 24, 2009 I know whats wrong here.. but why is it wrong... <? $CC = ".UserNameHere"; preg_match("/^(\.|!|#|@|\+|^|\$|-|_|~)([^\s]+)\s?(.+)$/i", $CC, $CCM); echo "<pre>"; print_r($CCM); echo "</pre>"; ?> This should come out false because there is nothing after the space.... not move the last e in Username over there.... can anyone explain this in great detail how greed/lazyness works.. the tutorial didn't explain its workings great enough... once i understand this im set... please thank also.. why does ? make it like not count in a regex.. it means 0 or 1.. not don't count Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted September 24, 2009 Share Posted September 24, 2009 What you have to understand is that regex will backtrack if need be.. So in a nutshell, it will match what it can.. but if there is more in the pattern to satisfy after the entire string is matched/captured, regex will need to relinquish previously matched characters from the previous match/capture to accommodate what is left in the pattern. So your array index[0] will match the whole thing - Check. The first capturing group is (.|!|#|@| |^|$|-|_|~).. so this will translate into index[1] capturing the dot. - Check. The second capturing group is ([^s]+).. so this will translate into index[2] capturing 'UserNameHere' at this point.. so in other words, the rest of the string (as there is no spaces in the rest of the string, the rest is captured).. -but- there is more in the pattern after ([^s]+). So this means regex will now have to backtrack, relinquishing characters from the previous capture to make room for the next capture in the string.. since the last capturing group is (.+), the final 'e' in 'UserNameHere' is relinquished from index[2] (so now, index[2] = UserNameHer) and that final 'e' is now stored into the final array index. I would recommend getting this book if you really want to get on the ball with regards to how regex engines work. You can also learn more about regex in the following links: http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax http://www.regular-expressions.info/ http://weblogtoolscollection.com/regex/regex.php Side note.. use character classes instead of alternations for single characters.. it's faster.. so instead of say (a|b|c), use [abc] Quote Link to comment Share on other sites More sharing options...
shane18 Posted September 24, 2009 Author Share Posted September 24, 2009 thanks a lot for that VERY IMPORTANT information.. ill pass on the book i bought a html/css&javascript&php/mysql book if i can't learn it outa there ill learn it experimenting lol.. the php book did eregi not preg_match.... but thats ok.. the rest of it is awesome Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted September 24, 2009 Share Posted September 24, 2009 thanks a lot for that VERY IMPORTANT information.. ill pass on the book i bought a html/css&javascript&php/mysql book if i can't learn it outa there ill learn it experimenting lol.. the php book did eregi not preg_match.... but thats ok.. the rest of it is awesome You would definitely learn a lot by experimenting for sure. Plenty of trial and error never killed anyone (well, not at a desk writing webcode anyway.. at least, I don't think so). As for the book you mentioned, if it discusses ereg, then it is not the regex system to learn for sure (as POSIX is now technically depreciated [as of PHP 5.3], and will not be included in the core as of version 6). It all really boils down to how much time you invest into things (like regex for example). The more you play around with it, the more comfortable you become (not meant as a sexual innuendo ). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.