?
Solved

Do a logical AND in REGULAR EXPRESSIONS

Posted on 2010-08-18
7
Medium Priority
?
544 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 93

Expert Comment

by:Patrick Matthews
ID: 33470055
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
ID: 33470104
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 85

Expert Comment

by:ozo
ID: 33470975
(?=.*PETS)(DOGS|CATS|GOLDFISH)
matches the same strings as
(DOGS|CATS|GOLDFISH).*PETS
Perhaps you meant
(?=.*PETS).*(DOGS|CATS|GOLDFISH)
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 35

Expert Comment

by:Terry Woods
ID: 33470986
Thanks ozo
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33471260
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 1996 total points
ID: 33474462
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
ID: 33476515
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

862 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