Jump to content

if contains(haystack, 'needle') || contains('haystack, another needle') regex?


dsaba

Recommended Posts

Reading about negative lookaheads and postive lookaheads, trying to construct a regex pattern that matches:

 

any string 'this' any string 'that'

 

so it should not match:

any string 'this' any string 'this' 'that

 

and it should also not match:

any string 'that' any string 'that' 'this'

 

but it can match:

any string 'that' any string 'this'

 

so you see it should have the needles 'this' and 'that' but not this or that repeating itself

 

I tried doing this:

<?php
$pat = '~.(??!(this|that)).)+this(??!(this|that)).)+that.~';
$haystack = 'hello how are you this doing today that maybe whatever this this that should not match, but that okay later this should, but no that of that of this okay?';
preg_match_all($pat, $haystack, $out);
print_r($out);
?>

 

but it matches 'whatever this this' when it shouldn't

 

how can I fix it?

-thanks

 

Update by effigy: Changed $haystrack to $haystack in the code.

Link to comment
Share on other sites

hi to explicate what i'm trying to do in regex, I made this well described example, so please ignore my previous question because this question now with this example is the one I'd like to see written in regex

 

I've been reading about lookaheads and lookbehinds, but have not succeeded in creating regex to do this, so I'd like to see an example of this, so I can learn from it, thank you

 

I want to match phrases that start and end with 'small' 'medium' or 'big' and only contain 1 of each inside the matched phrase

So you can match 'small medium big' or 'big small medium' but not 'small medium medium big' or 'small small big medium'

The entire matched phrase that contains 1 of each of these words must also not be longer than 500 characters.

 

Example Haystack:

once there was a small house where there lived a medium sized man who was married to a big woman and her hair was also big but her hands were small yet the man's apathy towards this was small in medium amounts because of his big heart and he had big dreams in medium spurts of time yet small was the times he pursued them so they called him small medium and big once upon a small time ago in this big story of medium medium stuff

 

matches

'small house where there lived a medium sized man who was married to a big'

'small in medium amounts because of his big'

'big dreams in medium spurts of time yet small'

'small medium and big'

'small time ago in this big story of medium'

 

I made this regex tester which you can use if you want:

http://nancywalshee03.freehostia.com/regextester/regex_tester.php

Link to comment
Share on other sites

please ignore my previous question

 

I started working on it before you posted this, and I enjoyed the challenge, so I continued. The result is below. I will look at your most recent post as time allows.

 

<pre>
<?php
$haystack = 'hello how are you this doing today that maybe whatever this this that should not match, but that okay later this should, but no that of that of this okay?';
preg_match_all('/
	\b
	(??!(?:this|that)).)+
	(this|that)
	(??!(?:this|that)).)+
	(?!\1)(?:this|that)
/x', $haystack, $out);
print_r($out);
?>
</pre>

 

Result:

        (

            [0] => hello how are you this doing today that

            [1] =>  this that

            [2] =>  should not match, but that okay later this

            [3] =>  of that of this

        )

Link to comment
Share on other sites

Here goes:

 

<pre>
<?php
$data = <<<DATA
once there was a small house where there lived a medium sized man who was married to a big woman and her hair was also big but her hands were small yet the man's apathy towards this was small in medium amounts because of his big heart and he had big dreams in medium spurts of time yet small was the times he pursued them so they called him small medium and big once upon a small time ago in this big story of medium medium stuff
DATA;
//'
preg_match_all('/
	\b
	(small|medium|big)
	(??!(?:small|medium|big)).)+
	(?!\1)(small|medium|big)
	(??!(?:small|medium|big)).)+
	(?!\1|\2)(?:small|medium|big)
/x', $data, $out);
print_r($out);
?>
</pre>

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.