RegEx - How to match two words in a string?

Hi All,

I need to write a regex that matches two words, separated by an arbitrary number of other characters, but I can't seem to get it to work.

Example:

BigString = "uhfkjbvfkj+ant+ohgrkjhgr+bird+riouhrh"

Search terms = "ant" "bird"

I thought the following regex would work:

.*ant.*bird.*

but according to this site:

http://sqa.fyicenter.com/Online_Test_Tools/Test_Regular_Expression_Match_Pattern.php

it doesn't match.

My interpretation of the regex I used is:

{AnyString} followed by "ant" followed by {AnyString} followed by "bird" followed by {AnyString}

which should work, but obviously I am not getting it!

Please can you advise how to write the regex, and also explain where I am misunderstanding?

Thanks,

Alan.
LVL 23
AlanConsultantAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Terry WoodsConnect With a Mentor IT GuruCommented:
If you don't want the order of the two words to matter, you can use this to detect a match, provided your regex tool of choice supports lookahead:

/(?=.*\bant\b)(?=.*\bbird\b)/i
0
 
sshah254Commented:
Try (.*)ant(.*)bird

SS
0
 
Pratima PharandeConnect With a Mentor Commented:
try this

/.*ant.*bird.*/
0
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.

 
user_nCommented:
Try the same but with /.*ant.*bird.*/
0
 
AlanConsultantAuthor Commented:
Hi All,

SS:  That doesn't seem to work?  Without the trailing wildcard, won't it require that the string ends in "bird"?  I tested it on the site I linked to, and it seems to fail the example I gave.

Pratima / UserN:

Those work exactly as required.  Why do they work, and what is wrong with mine?

Ozo:  I used that site again, and it doesn't work for me - it says, "No match found".  Are you getting a different result there?


Thanks,

Alan.

0
 
käµfm³d 👽Connect With a Mentor Commented:
You shouldn't really need the starting and ending .*  ...  "ant.*bird" should suffice.

Those work exactly as required.  Why do they work, and what is wrong with mine?
The site requires pattern delimiters. These are the two slashes that you have seen other mention. Some languages require the use of pattern delimiters: Perl and PHP, for example. If you were entering simply:

    .*ant.*bird.*

Then you wouldn't match. But if you entered:

    /.*ant.*bird.*/

then you should have matched, depending on whether or not the case of both your pattern and target string matched. To turn on case-insensitivity, you could have done:

    /.*ant.*bird.*/i

The trailing "i" enables case insensitivity.

Incorporating this information with my earlier suggestion, you can simply use:

    /ant.*bird/i
0
 
user_nConnect With a Mentor Commented:
http://www.regular-expressions.info/php.html

All of the preg functions require you to specify the regular expression as a string using Perl syntax. In Perl, /regex/ defines a regular expression. In PHP, this becomes preg_match('/regex/', $subject). When forward slashes are used as the regex delimiter, any forward slashes in the regular expression have to be escaped with a backslash. So http://www\.jgsoft\.com/ becomes '/http:\/\/www\.jgsoft\.com\//'. Just like Perl, the preg functions allow any non-alphanumeric character as regex delimiters. The URL regex would be more readable as '%http://www\.jgsoft\.com/%' using percentage signs as the regex delimiters, since then you don't need to escape the forward slashes. You would have to escape percentage sings if the regex contained any.
0
 
käµfm³d 👽Commented:
One thing to keep in mind when using any of the above suggestions is that you won't be limited to just capturing "words". You will actually find any string that contains the string "ant" followed at some point by the string "bird". Any of the above will do this. If you had the following:

    Put the canteen on top of the birdhouse.

You would still match:

    Put the canteen on top of the birdhouse.

If you want to match actual words, then I would suggest using some bounding conditions, such as "word boundary": \b

/\bant\b.*\bbird\b/i

Open in new window


Now the example sentence above would not match, because "ant" and "bird" do not occur on word boundaries. However, if you had the sentence:

    The ant was eaten by the bird.

You would find a match.
0
 
AlanConsultantAuthor Commented:
Thanks All - Very interesting stuff!

Alan.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.