Regular Expression question

Posted on 2008-10-16
Last Modified: 2012-05-05
I need to obtain a Regular expression pattern which will do the following-
Given an input string, return true if it contains the words APPLE and/or ORANGE but not the word MANGO. The words may appear in any order in the string.

Input : " I like APPLE and MANGO"        Output : False
Input : "I like APPLE and ORANGE"        Output : True
Input : "I like ORANGE"                          Output : True
Input : "I like MANGO"                           Output : False
Question by:CuteBug
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
LVL 20

Expert Comment

ID: 22738299
Try this
LVL 16

Author Comment

ID: 22738362
In this case, the first example will fail (as APPLE is present and so is MANGO, in which case it should have returned false)
LVL 51

Expert Comment

ID: 22738430
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!

LVL 27

Expert Comment

ID: 22738980

Regex re = new Regex(@"(?=.*(APPLE|ORANGE))(?!.*MANGO).*",RegexOptions.Singleline);

Open in new window

LVL 27

Accepted Solution

ddrudik earned 500 total points
ID: 22738991
And with the addition of the ^ and $ for string boundaries.
Regex re = new Regex(@"^(?=.*(APPLE|ORANGE))(?!.*MANGO).*$",RegexOptions.Singleline);

Open in new window

LVL 16

Author Closing Comment

ID: 31507051
Thanks a lot!
LVL 27

Expert Comment

ID: 22739066
Thanks for the question and the points.

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

617 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