Reputation: 428
I need to match a string with the following pattern: "ExactWord (Pattern)"
Pattern consists of one or more words, each separated by a single whitespace or a single hyphen. If a whitespace or hyphen is found immediately after open bracket, it should not match. If a whitespace or hyphen is found immediately before close bracket it should not match.
My current problem is that I cannot write a pattern that won't match multiple spaces and multiple hyphens:
using System;
using System.Text.RegularExpressions;
namespace Code
{
public static class Program
{
public static void Main()
{
string[] str = new string[] { "CorrectWord (black-tailed jackrabbit)",
"IncorrectWord (saber-tooth)",
"CorrectWord(animal)",
"CorrectWord bird",
"CorrectWord (Lion)",
"CorrectWordNot (Eagle)",
"CorrectWord (Harpy eagle)",
"CorrectWord (One Two Three)",
"CorrectWord (Too Many Spaces)", //should not match, but does.
"CorrectWord (One-two-three)",
"CorrectWord (Too--Many Hypens)", //should not match, but does
"CorrectWord ( leading-white-space)",
"CorrectWord (trailing white space )"
};
foreach (string s in str)
{
if (Regex.IsMatch(s, @"^CorrectWord" + Regex.Escape(" (") + @"\w+[\w+(\s|\-)?]+\w+" + Regex.Escape(")") + "$"))
{
Console.WriteLine(s);
}
}
}
}
}
Output:
CorrectWord (black-tailed jackrabbit)
CorrectWord (Lion)
CorrectWord (Harpy eagle)
CorrectWord (One Two Three)
CorrectWord (Too Many Spaces)
CorrectWord (One-two-three)
CorrectWord (Too--Many Hypens)
Upvotes: 1
Views: 1177
Reputation: 186803
You can try
\ACorrectWord \([A-Za-z]+(?:[ -][A-Za-z]+)*\)\z
pattern; here
\A - anchor, string start
CorrectWord - correct word as it should be
- ' ' (space)
\( - '('
[A-Za-z]+ - word (one or more chars in A..Z or a..z range)
(?: ... )* - followed by zero or more chunks which are
[ -][A-Za-z]+ - hyphen or space [ -] then word [A-Za-z]+
\) - ')'
\z - anchor, end of the string
Demo:
string[] str = new string[] {
"CorrectWord (black-tailed jackrabbit)",
"IncorrectWord (saber-tooth)",
"CorrectWord(animal)",
"CorrectWord bird",
"CorrectWord (Lion)",
"CorrectWordNot (Eagle)",
"CorrectWord (Harpy eagle)",
"CorrectWord (One Two Three)",
"CorrectWord (Too Many Spaces)", //should not match, but does.
"CorrectWord (One-two-three)",
"CorrectWord (Too--Many Hypens)", //should not match, but does
"CorrectWord ( leading-white-space)",
"CorrectWord (trailing white space )"
};
Regex regex = new Regex(@"\ACorrectWord \([A-Za-z]+(?:[ -][A-Za-z]+)*\)\z");
var result = str
.Select(test => $"{test,-40} :: {(regex.IsMatch(test) ? "match" : "failed")}");
Console.Write(string.Join(Environment.NewLine, result));
Outcome:
CorrectWord (black-tailed jackrabbit) :: match
IncorrectWord (saber-tooth) :: failed
CorrectWord(animal) :: failed
CorrectWord bird :: failed
CorrectWord (Lion) :: match
CorrectWordNot (Eagle) :: failed
CorrectWord (Harpy eagle) :: match
CorrectWord (One Two Three) :: match
CorrectWord (Too Many Spaces) :: failed
CorrectWord (One-two-three) :: match
CorrectWord (Too--Many Hypens) :: failed
CorrectWord ( leading-white-space) :: failed
CorrectWord (trailing white space ) :: failed
Upvotes: 3