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! Link to comment https://forums.phpfreaks.com/topic/171922-reg-ex-problem-extract-the-string-inside/ 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 Link to comment https://forums.phpfreaks.com/topic/171922-reg-ex-problem-extract-the-string-inside/#findComment-906551 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); Link to comment https://forums.phpfreaks.com/topic/171922-reg-ex-problem-extract-the-string-inside/#findComment-906574 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. Link to comment https://forums.phpfreaks.com/topic/171922-reg-ex-problem-extract-the-string-inside/#findComment-906579 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. Link to comment https://forums.phpfreaks.com/topic/171922-reg-ex-problem-extract-the-string-inside/#findComment-906683 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. Link to comment https://forums.phpfreaks.com/topic/171922-reg-ex-problem-extract-the-string-inside/#findComment-906692 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. Link to comment https://forums.phpfreaks.com/topic/171922-reg-ex-problem-extract-the-string-inside/#findComment-906777 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. Link to comment https://forums.phpfreaks.com/topic/171922-reg-ex-problem-extract-the-string-inside/#findComment-906794 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.