Solved

Do a logical AND in REGULAR EXPRESSIONS

Posted on 2010-08-18
7
501 Views
Last Modified: 2013-11-09
I need a regular expression to return matches only IF a keyword appears in a string AND one or more match words also appear in the string.

Here is an example.  I want to return matches for the words "DOGS", "CATS", "GOLDFISH" but only if the word "PETS" is in the string  

For example, the sentence "CATS, DOGS AND GOLDFISH ARE POPULAR PETS" would return "CATS  DOGS GOLDFISH".

The sentence "DOGS, CATS AND GOLDFISH ARE TYPES OF ANIMALS" would not return anything because PETS is not in the string.

Is there a way to do this with a regular expression?  I'm new to regular expressions and would have never dreamed it would be so difficult.  I can match words no problem but I can't code the conditional AND to have it not show anything if a given word is not there.

This has drove me crazy all afternoon.  Thanks in advance for advice...

I will ultimately be using the expression from VB 2005 using the .net regex library.
0
Comment
Question by:LiebertUser
7 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
No need for regexp here as simple sql does it easily.

Use a where clause such as:

Where somecolumn like '%pets%' and (somecolumn like '%dogs%' or somecolumn like '%cats%' or somecolumn like '%goldfish%')
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
It's possible using a regex if the flavour of regular expression supports lookaheads, which .NET does. It's done like this:

(?=.*PETS)(DOGS|CATS|GOLDFISH)
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
(?=.*PETS)(DOGS|CATS|GOLDFISH)
matches the same strings as
(DOGS|CATS|GOLDFISH).*PETS
Perhaps you meant
(?=.*PETS).*(DOGS|CATS|GOLDFISH)
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
Thanks ozo
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
I'm as big a fan of RegExp as anyone, but I suspect relying on native SQL Server functionality will perform better in this instance :)

Turning on full text indexing and using the contains function may do even better:

http://www.developer.com/db/article.php/3446891/Understanding-SQL-Server-Full-Text-Indexing.htm
0
 
LVL 4

Accepted Solution

by:
SyfAldeen earned 499 total points
Comment Utility
This is the correct regular expression.
TerryAtOpus, your RegEx will fail in Example 3!
ozo, you've spoiled TerryAtOpus' RegEx instead of fixing it!

(?:(?=.*PETS)|(?<=PETS.*))(?:DOGS|CATS|GOLDFISH)

Open in new window

RegEx.png
0
 

Author Closing Comment

by:LiebertUser
Comment Utility
Thanks SyfAldeen, TerryAtOpus and ozo.  Don't be sore at me because I accepted SyfAldeen's solution.  I was so stumped that all of the Regex solutions worked good for me but SyfAldeen's Regex did in fact catch the 3rd test string where the first two didn't.
I need to read more on Regular Expressions but this was one time I was so flustered because nothing I did worked.  I will learn so much more by dissecting your working example.  
P.S.  I do have a real production use for this regex that involves part#s etc, but instead of clouding the question with our data nomenclature I hoped kitties and dogs would make more sense.  Apperently it did!  THANKS AGAIN!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now