• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 453
  • Last Modified:

REGEX> Return True if you find CHUCK NORRIS

#Complicated string
$string='I\'m going to chuck this thai  tea drink, today is 01-6-2010  because it tastes like thai tea and Norris says it also tastes like an aluminum can';

#What I need to find: I need this to return a positive if it finds both of these two words in the string: Chuck, Norris. Case insensitive, yet do nothing if it only finds part of Chuck Norris.

#Must use preg_replace even though it isn't ideal. Just you preg_replace and regex.

#I'd prefer it to find it in the order of Chuck preceding Norris, not vice versa. But it's ok if it searches both ways.
#Complicated string
$string='I\'m going to chuck this thai  tea drink, today is 01-6-2010  because it tastes like thai tea and Norris says it also tastes like an aluminum can';

#What I need to find: I need this to return a positive if it finds Chuck and Norris.. Case insensitive, yet do nothing if it only finds part of Chuck Norris.

#Must use preg_replace even though it isn't ideal. Just you preg_replace and regex.

#I'd prefer it to find it in the order of Chuck preceding Norris, not vice versa. But it's ok if it searches both ways.

Open in new window

0
www_puertoricoautoforo_com
Asked:
www_puertoricoautoforo_com
  • 3
  • 3
  • 3
  • +1
4 Solutions
 
Gurvinder Pal SinghCommented:
something like this?
<?php
if (preg_match("CHUCK|NORRIS", "Hello World!", $matches)) {
  echo "Match was found <br />";
  echo $matches[0];
}
?>
0
 
www_puertoricoautoforo_comAuthor Commented:
I think that would just find the word CHUCK or NORRIS but not both, but what I need is a true return only if CHUCK and NORRIS exist in the $string.

Here is an example of the preg_replace stuff..


function vehicle_model($input)
    {
        $input = strtolower($input);
        $input = preg_replace('~\s{2,}~', ' ', $input);
        $input = ' '.$input.' ';
            #Acura
            $patterns['chuckfinder'] = '/.* (chuck|norris) .*/';
            $replacements['chuckfinder'] = 'We found him';
        $input=preg_replace($patterns, $replacements, $input);
        return $input;
    }

$input='I\'m going to chuck this thai  tea drink, today is 01-6-2010  because it tastes like thai tea and Norris says it also tastes like an aluminum can';

echo chuckfinder($input);

Open in new window

0
 
www_puertoricoautoforo_comAuthor Commented:
oops.  I mean "chuckfinder" instead of "vehicle_model" on line1 for the function name.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
clintonljCommented:
I think that your regular expresstion should look as follows:

"(Chuck)+[\s]+(Norris)+"

When you specify the Options use :IgnoreCase
This will make it case insensitive...
Explanation: Find One or More instanses of a group that consists of the character string "chuck" and followed by one or more white spaces and then followed by one or more instances  of a group that consists of the character string "norris"... Well, hope this helps...
0
 
Gurvinder Pal SinghCommented:
the pattern would be
CHUCK|NORRIS|CHUCK(.*\s*)*NORRIS|NORRIS(.*\s*)*CHUCK
0
 
www_puertoricoautoforo_comAuthor Commented:
doesn't [\s] just mean one or more spaces?

Would this find chuck and norris if there was a bunch of text and numbers between the two?
0
 
Gurvinder Pal SinghCommented:
yes. It would find that. Is it not what you want?

If that is not what you want then try this

CHUCK|NORRIS|CHUCK\sNORRIS|NORRIS\sCHUCK
or
CHUCK|NORRIS|CHUCK\s*NORRIS|NORRIS\s*CHUCK        \\if more than one space is allowed
0
 
käµfm³d 👽Commented:
Chuck Norris cannot be found by using a regular expression. If a regular expression did find Chuck Norris, he would punch it in the face and it would die... and so no one would ever know that the regular expression succeeded.

:)
0
 
käµfm³d 👽Commented:
Your pattern would be:
/chuck.+?norris/i

Open in new window

0
 
käµfm³d 👽Commented:
@ www_puertoricoautoforo_com
>> doesn't [\s] just mean one or more spaces?

No. It means one space. \s is equivalent to [\s]. One or more spaces would be \s+

@ gurvinder372
>> CHUCK|NORRIS|CHUCK\s*NORRIS|NORRIS\s*CHUCK        \\if more than one space is allowed

\s* means 0 or more spaces, not 1 or more spaces
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now