Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

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
0
CuteBug
Asked:
CuteBug
1 Solution
 
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
 
ahoffmannCommented:
(?!MANGO)(APPLE|ORANGE)
0
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.

 
ddrudikCommented:

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

Open in new window

0
 
ddrudikCommented:
And with the addition of the ^ and $ for string boundaries.
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now