Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

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.
0
Alan
Asked:
Alan
  • 2
  • 2
  • 2
  • +4
4 Solutions
 
sshah254Commented:
Try (.*)ant(.*)bird

SS
0
 
Pratima PharandeCommented:
try this

/.*ant.*bird.*/
0
Independent Software Vendors: 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!

 
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 👽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_nCommented:
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
 
Terry WoodsIT 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
 
AlanConsultantAuthor Commented:
Thanks All - Very interesting stuff!

Alan.
0

Featured Post

Industry Leaders: 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!

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