Another regex question

nbrege

Active member
Joined
Jan 12, 2006
Messages
34
I want to check a string to see if it contains (2) specific words. ie. if the string contains "red" & "blue" anywhere in it, in any order, then return true.
Can someone show me a regex to do this? Thanks...
 
Nerseus said:
Does it have to be regex? Sounds like you just want IndexOf twice?

-ner
I was going to suggest that earlier, however alot of it will depend on what the initial string may look like. If for example you know its going to be a list of colours then itll work ok. But IndexOf will pick up red from say hatred or rediculous you could always put spaces either side but that will cause trouble if you have any punctuation in the input string.
 
How do I use IndexOf? Im not familiar with it. Im currently using .Contains twice, but I thought maybe a regex would be better.
 
nbrege said:
How do I use IndexOf? Im not familiar with it. Im currently using .Contains twice, but I thought maybe a regex would be better.
If contains works stick with it, theres no point in making something more complicated than it needs be.
C#:
string input = "this is the string I want to test for red and blue";

if(input.IndexOf("red") != -1 && input.IndexOf("blue") != -1)
{
        // the string contains both values
}
IndexOf() returns an integer of the first place it finds the search parameter, if it returns -1 then the value isnt found.
 
C#:
string input = "this is the string I want to test for red and blue";

Regex regex = new Regex("[Rr]ed|[Bb]lue");

foreach (Match match in regex.Matches(input))
{
    Console.WriteLine(match.Value + " at index " + match.Index);
}
 
I dont think the regex with that loop will cover all cases. Is there a single solution that will return true/false for IsMatch()? I dont have a counter solution yet, but Ill see what I can turn up. The "hatred" problem is giving me issues. Are the following cases correct or am I making this more complicated than it needs to be?

Should all return true:
1. "red blue"
2. "red and blue"
3. "this is red and this is blue"
4. "blue red"
5. "blue and red"
6. "this is blue and this is red"
7. "red blue and more words"
8. "more words red blue more words"
9 "more words red and more and blue and more"
10. "blue red and more words"
11. "more words blue red more words"
12. "more words blue and more and red and more"

Should all return false:
1. "duh"
2. "red"
3. "blue"
4. "redblue"
5. "this contains blue and blue"
6. "This regex for blue causes hatred"
...

Unless there is a trick I have forgotten, the hard part is ingnoring words that have r-e-d in them while still getting the start and end cases where the string starts with either red or blue.
 
Why not something like this?

\bred\b.*\bblue\b|\bblue\b.*\bred\b

The word boundary escape is pretty magical. It makes difficult things very easy. The regex is basically this: either "red" as a word, followed by "blue" as a word, with anything or nothing in between, or the other way around.

It would require a single line option and case-insensitivity to work, but it should do the trick. I used all of mskeels test phrases and got the expected results.
 
To be fair, \b is documented in MSDN to be an escape for the backspace character. A search for regular expressions "word boundary" in the .Net 2005 help yields very little helpful information. You could browse documentation all day and never find it.
 
Just out of interest, does this cope with punctuation? I assume it does since its called a word boundary. I would have checked it out myself, but Ive never used a regular expression. For example would it accept the following inputs as valid...

"my favourite colours are red, blue and purple."
"with a red face the boy replied "blue"."
"do you prefer red or blue?"
 
Yes, it works fine with punctuation. All of the following should work:
"Red/White/Blue"
"Purple has a red-blue hue"
"(blue)red"
"System.Drawing.Colors.Blue, System.Drawing.Colors.Red"
 

Similar threads

Back
Top