burtybob Posted October 25, 2009 Share Posted October 25, 2009 Hi, I don't know if this should be here or in the regex help section so if it is in the wrong section please move it. I have this preg_match("/\/warn (.*)/",$shout,$matches) I understand almost everything in there except the ""/\/warn (.*)/"" section... It takes input from a shoutbox input. So you put in "/warn burtybob" and it will warn burtybob and I understand all the rest of the script that does that. I would like to know what the "/\" is for, I presume it is something to do with character escaping. I would like to know what the "(.*)/" stuff does as well. I would like to say thanks in advance for explaning this to me. Thanks in advance, Ben Quote Link to comment Share on other sites More sharing options...
Mchl Posted October 25, 2009 Share Posted October 25, 2009 It's so called 'regula expression pattern' - a sting that tells preg_match function what to look for. For quite good tutorial on this head here: http://www.regular-expressions.info/ Quote Link to comment Share on other sites More sharing options...
cags Posted October 25, 2009 Share Posted October 25, 2009 The first forward slash is the opening delimiter, all preg functions will have an opening and closing delimiter. the \/ is an escaped forward slash. This means to take it as a literal character ie match "/warn" the brackets of the next part tells it to capture it as a seperate group in the $mathces array. The . means 'any character' (with a few exceptions) and the * means repeat the character before 0 or more times. Then the final forward slash is the closing delimiter. Checkout the link provided by Mchl to learn about regular expressions. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted October 25, 2009 Share Posted October 25, 2009 @burtybob This is why I don't like using the forward slash as delimiters, as that character is common for paths and whatnot. Using something other than the forward slash negates the need to escape them elsewhere in the pattern.. I typically use the hash(#) as my delimiters.. but you can choose from an assortment of delimiters, like ~....~ or !...!, etc.. There's a caveat on using .* This is typically frowned upon for potential speed / accuracy purposes. You can read up about that (in the form of .+, but pretty much the same principal) here (post #14 illustrates the pitfalls). 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.