AndyPSV Posted December 21, 2011 Share Posted December 21, 2011 I've got: dsadsasddsasda @Andrzej Jeziorski: z\dsadsadsadsasd @Karol Orzechowski: I want to get all between signs: "@:", typically array( 0 => 'Andrzej Jeziorski', 1 => 'Karol Orzechowski' ) thank you Quote Link to comment Share on other sites More sharing options...
premiso Posted December 21, 2011 Share Posted December 21, 2011 $string = 'dsadsasddsasda @Andrzej Jeziorski: z\dsadsadsadsasd @Karol Orzechowski:'; preg_match_all('#@(.*?):#s', $string, $matches); print_r($matches); Outputs: Array ( [0] => Array ( [0] => @Andrzej Jeziorski: [1] => @Karol Orzechowski: ) [1] => Array ( [0] => Andrzej Jeziorski [1] => Karol Orzechowski ) ) Quote Link to comment Share on other sites More sharing options...
xyph Posted December 22, 2011 Share Posted December 22, 2011 A slightly faster RegEx would be @[^:]++ The ++ doesn't allow the engine to backtrack. The character class matches anything that's not a : I also removed the capturing group, since we can use string functions to strip the @ off of the result. It saves us from storing almost the exact same string twice in memory. Quote Link to comment Share on other sites More sharing options...
.josh Posted December 22, 2011 Share Posted December 22, 2011 A slightly faster RegEx would be @[^:]++ The ++ doesn't allow the engine to backtrack. The character class matches anything that's not a : I also removed the capturing group, since we can use string functions to strip the @ off of the result. It saves us from storing almost the exact same string twice in memory. I don't think the ++ is necessary though...it shouldn't ever backtrack because there's nothing else in the pattern to match for. Also, to 1up you on using something like trim to get rid of the @...can use \K to have the regex engine ditch it: @\K[^:]+ Quote Link to comment Share on other sites More sharing options...
ragax Posted December 22, 2011 Share Posted December 22, 2011 Wow, beautifully intricate discussion for such a simple match, I love it! I thought it was all over in ten minutes after premiso's reply, but no! @.josh, thank you for mentioning \K. I'd read about it without really paying attention as I was on 5.2.1, but recently upgraded to 5.3.8 which supports it. Thank you for pointing it out, I'm going to start using it. Not yet supported in RegexBuddy by the way, but I found a thread from 8 months ago where Jan (the developer) says it's on the todo list. Wishing you all a fun day. 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.