fluidsharp Posted January 7, 2010 Share Posted January 7, 2010 Hello. I'd like choice "hello world" pairs from string like: hello dfgdfg hello eferf world hello ffewf hello dfefer erer efer hello erferf erferf btbtyhg world there is two pairs. Formula must be universal for different positions pairs. I can't make regex formula. I've tried \b\w*((\bhello)(\b\w*)\bworld)\b\w* but it incorrect. Please help. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/187616-fimd-pairs/ Share on other sites More sharing options...
salathe Posted January 7, 2010 Share Posted January 7, 2010 The main issue is that the pattern matches when there are only "word" characters between hello and world. In your example above, there are multiple non-word characters (spaces) there. Quote Link to comment https://forums.phpfreaks.com/topic/187616-fimd-pairs/#findComment-990537 Share on other sites More sharing options...
fluidsharp Posted January 7, 2010 Author Share Posted January 7, 2010 but why? \b\w* - match the all words in a string. I'm looking to rtfm but solution haven't made up yet. Could you give me once more suggestion? ((hello)([^\w*])world) - it match nearest need words.... Quote Link to comment https://forums.phpfreaks.com/topic/187616-fimd-pairs/#findComment-990617 Share on other sites More sharing options...
fluidsharp Posted January 7, 2010 Author Share Posted January 7, 2010 \bhello\b(.*?)\bworld\b it's better but match all between hello world... Quote Link to comment https://forums.phpfreaks.com/topic/187616-fimd-pairs/#findComment-990661 Share on other sites More sharing options...
nrg_alpha Posted January 8, 2010 Share Posted January 8, 2010 but why? \b\w* - match the all words in a string. Actually, no, it wouldn't. What you have to understand is that the star (zero or more) quantifier only applies to \w.. so future spaces in the sentence would not be included. By grouping both the word boundery and word character class in some form of grouping, like capturing: (\b\w)* or non capturing: (?:\b\w)*, this would encompass both (zero or more times). If I understand what you are looking for, here would be my take on this: $str = 'hello dfgdfg hello get this part world hello ffewf hello get this part as well world fgjhe hello world uiuouo hello and finally this part world.'; preg_match_all('#\bhello(?:.(?!\bhello))+\bworld#', $str, $matchedWords); echo '<pre>'.print_r($matchedWords, true); //Output: //Array //( // [0] => Array // ( // [0] => hello get this part world // [1] => hello get this part as well world // [2] => hello world // [3] => hello and finally this part world // ) //) I'm not sure if this is along the lines of what you are looking for or not. [ot] Don't mind the format of the array, as this is caused because of the echo '<pre>'.print_r($matchedWords, true); line. I used that just for quick (yet presentable) array outputting. You could use a simple foreach loop instead of print_r. [/ot] Quote Link to comment https://forums.phpfreaks.com/topic/187616-fimd-pairs/#findComment-990701 Share on other sites More sharing options...
fluidsharp Posted January 8, 2010 Author Share Posted January 8, 2010 thanks, but I would match without strings between "hello world". I'll try do it oneself and reread man. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/187616-fimd-pairs/#findComment-991156 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.