Jump to content

Fimd pairs


fluidsharp

Recommended Posts

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.

 

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.