shane18 Posted September 24, 2009 Share Posted September 24, 2009 I'm trying to make it so I can extract the Command Symbol, User Name, and Reason from a string. This will allow me to control my site w/o making a lot of pages/forms so please help. <? $CC = ".UserNameHere Reason Here"; preg_match("/^(\.|!|#|@|\+|^|\$|-|_|~)(.+?)\s?(.*)$/", $CC, $CCM); echo "!----".$CCM[0]."-----".$CCM[1]."-----".$CCM[2]."----".$CCM[3]."----".$CCM[4]."----".$CCM[5]."----!"; ?> **NOTE** The Reason is optional that is why I got it optional in the regexp. **NOTE** I get: !----.TEST BLAH BLAH-----.-----T----EST BLAH BLAH------------! Quote Link to comment Share on other sites More sharing options...
.josh Posted September 24, 2009 Share Posted September 24, 2009 $CC = ".UserNameHere Reason Here"; preg_match("/^([.!#@+^$_~-])([^\s]+)\s?(.*)$/", $CC, $CCM); echo "<pre>";print_r($CCM); Quote Link to comment Share on other sites More sharing options...
shane18 Posted September 24, 2009 Author Share Posted September 24, 2009 opps..... i really got: !----.UserNameHere Reason Here-----.-----U----serNameHere Reason Here------------! Quote Link to comment Share on other sites More sharing options...
shane18 Posted September 24, 2009 Author Share Posted September 24, 2009 that gave me Array ( [0] => .UserNameHere Reason Here [1] => . [2] => U [3] => serNameHere Reason Here ) Quote Link to comment Share on other sites More sharing options...
.josh Posted September 24, 2009 Share Posted September 24, 2009 this is what I get: Array ( [0] => .UserNameHere Reason Here [1] => . [2] => UserNameHere [3] => Reason Here ) Quote Link to comment Share on other sites More sharing options...
shane18 Posted September 24, 2009 Author Share Posted September 24, 2009 oh i didn't notice you also edited the regex lol space.. thanks Quote Link to comment Share on other sites More sharing options...
shane18 Posted September 24, 2009 Author Share Posted September 24, 2009 can you explain to me what a negative class does exactly. Quote Link to comment Share on other sites More sharing options...
.josh Posted September 24, 2009 Share Posted September 24, 2009 it matches one of anything not specified. So for example [^a] will match any one character that is not an 'a' Quote Link to comment Share on other sites More sharing options...
.josh Posted September 24, 2009 Share Posted September 24, 2009 In the regex I provided specifically: [^\s]+ means match 1 or more of anything that is not a space, tab or newline char. Quote Link to comment Share on other sites More sharing options...
shane18 Posted September 24, 2009 Author Share Posted September 24, 2009 1. why didn't what i posted work... it should of went to the first space.. then posted Username, then posted Reason Here... was it because of greedyness /lazyness? 2. so [^\s]+ will match until the first space then cancel? Quote Link to comment Share on other sites More sharing options...
.josh Posted September 24, 2009 Share Posted September 24, 2009 1. why didn't what i posted work... it should of went to the first space.. then posted Username, then posted Reason Here... was it because of greedyness /lazyness? Yes. Since your first .+? is lazy, and you aren't requiring the following space, and your last .* is greedy, the first .+? is satisfied by the single char match, and the last .* matches everything else. 2. so [^\s]+ will match until the first space then cancel? Yes. Or tab or newline. Quote Link to comment Share on other sites More sharing options...
shane18 Posted September 24, 2009 Author Share Posted September 24, 2009 Thanks alot, now 1 more question.. is it possible to make the whole string match not showed in the matchs.. just the sub groups Quote Link to comment Share on other sites More sharing options...
.josh Posted September 24, 2009 Share Posted September 24, 2009 The entire match is always shown in the first array element (element 0). I notice in your OP you have this: echo "!----".$CCM[0]."-----".$CCM[1]."-----".$CCM[2]."----".$CCM[3]."----".$CCM[4]."----".$CCM[5]."----!"; You could just remove it from your echo... Alternatively, you can array_shift $CCM Quote Link to comment Share on other sites More sharing options...
shane18 Posted September 24, 2009 Author Share Posted September 24, 2009 I though ? make it 0 or 1 times... not OPTIONAL ... are optional things ignored in the regex even if they're there. Quote Link to comment Share on other sites More sharing options...
.josh Posted September 24, 2009 Share Posted September 24, 2009 it effectively makes it optional. But technically it is the same mechanics as making a lazy quantifier. Instead of doing it on a "zero or more of this" (.*) or "one or more of this" (.+) you are doing a "one of this" (.?) And even reading it like "0 or 1" 'or' is a logical condition. Look at it in php terms: if (0 or 1) { ... } the condition is evaluated as true if the one on the left is true. php will not even look at the other side of that 'or'. 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.