Solved

Regular Expression question

Posted on 2008-10-16
7
316 Views
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.

Example:
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
0
Comment
Question by:CuteBug
7 Comments
 
LVL 20

Expert Comment

by:informaniac
Comment Utility
Try this
(APPLE|ORANGE)
0
 
LVL 16

Author Comment

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

Expert Comment

by:ahoffmann
Comment Utility
(?!MANGO)(APPLE|ORANGE)
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 27

Expert Comment

by:ddrudik
Comment Utility

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

Open in new window

0
 
LVL 27

Accepted Solution

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

Open in new window

0
 
LVL 16

Author Closing Comment

by:CuteBug
Comment Utility
Thanks a lot!
0
 
LVL 27

Expert Comment

by:ddrudik
Comment Utility
Thanks for the question and the points.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…

743 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

18 Experts available now in Live!

Get 1:1 Help Now