Solved

Do a logical AND in REGULAR EXPRESSIONS

Posted on 2010-08-18
7
511 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
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 84

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 35

Expert Comment

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

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 499 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
execute a MS SQL script as a schedule SQL job 72 128
Odds of picking games correctly 4 88
SQL query to summarize items per month 5 58
Strange msg in the SSMS pane 13 55
When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
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…
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 Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…

813 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

8 Experts available now in Live!

Get 1:1 Help Now