supernoobie Posted August 26, 2009 Share Posted August 26, 2009 I need help if Reg ex is able to extract the string, Here's my problem. I want to extract the string inside the word "View" and ends in "return". Sample: string = "View John Doe return" I want to get the result "John Doe" Is this possible in Regex? Thanks In advance! Quote Link to comment Share on other sites More sharing options...
ILMV Posted August 26, 2009 Share Posted August 26, 2009 Try this: $string="View Joe Bloggs return,, View James May return random text here View Bill Clinton return"; preg_match_all('/View\s([\w]+)\s([\w]+)\sreturn/',$string,$test); echo("<pre>".print_r($test,true)."</pre>"); ILMV Quote Link to comment Share on other sites More sharing options...
Garethp Posted August 26, 2009 Share Posted August 26, 2009 That's a bit complicated. If I understand correctly, all he needs is preg_match_all('~^View (.*+) return$~', $string, $Matches); Quote Link to comment Share on other sites More sharing options...
Adam Posted August 26, 2009 Share Posted August 26, 2009 That's a bit complicated. If I understand correctly, all he needs is preg_match_all('~^View (.*+) return$~', $string, $Matches); (.*+) should be: (.*?) or (.+?), depending on whether it must match '0 or more' characters or '1 or more', respectively. Edit: Also I can't see any real real point in using preg_match_all() with the start and end of string anchors. Quote Link to comment Share on other sites More sharing options...
supernoobie Posted August 26, 2009 Author Share Posted August 26, 2009 Thanks to all of you!! what if the name is separated with. coma like "View John,Doe ;return" All input is greatly appreciated. Thanks a lot. Quote Link to comment Share on other sites More sharing options...
Adam Posted August 26, 2009 Share Posted August 26, 2009 Try this: $str = 'View John,Doe ;return'; if (preg_match('/View(.*?);return/i', $str, $matches)) { print_r($matches); } You'll need to trim each match to remove any white space issues, but if this is user entered data you'd probably be better off trimming the data anyway; in-case they entered multiple spaces by accident. If this isn't user entered data you may wish to add the spaces back in like on the other examples. I've also added the 'i' modifier to make the expression case-insensitive - again if it's user entered data it's probably a good idea. Quote Link to comment Share on other sites More sharing options...
supernoobie Posted August 26, 2009 Author Share Posted August 26, 2009 Thanks A lot Mr. Adam, What if I want to get the the Age and the name like. "View John,Doe ;return Age:24" is this possible??? Again, thanks a lot for all of your help. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 26, 2009 Share Posted August 26, 2009 supernoobie.. I'm noticing a time wasting trend here.. you keep adding new circumstances after the previous solution. Perhaps providing the entire set of circumstances in your original post would be better, as this saves people time from having to constantly refine solutions... otherwise, in theory, you can keep creating new requests one after the other, which could all have been solved earlier on. 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.