?
Solved

Do a logical AND in REGULAR EXPRESSIONS

Posted on 2010-08-18
7
Medium Priority
?
531 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
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.
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

764 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