Jump to content

[SOLVED] Question About Regex Workings


shane18

Recommended Posts

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

Link to comment
Share on other sites

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]

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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  :P ).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.