Regular Expression question

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
LVL 16
CuteBugAsked:
Who is Participating?
 
ddrudikConnect With a Mentor Commented:
And with the addition of the ^ and $ for string boundaries.
Regex re = new Regex(@"^(?=.*(APPLE|ORANGE))(?!.*MANGO).*$",RegexOptions.Singleline);

Open in new window

0
 
informaniacCommented:
Try this
(APPLE|ORANGE)
0
 
CuteBugAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ahoffmannCommented:
(?!MANGO)(APPLE|ORANGE)
0
 
ddrudikCommented:

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

Open in new window

0
 
CuteBugAuthor Commented:
Thanks a lot!
0
 
ddrudikCommented:
Thanks for the question and the points.
0
All Courses

From novice to tech pro — start learning today.