Reputation: 35
I'm trying to create a regular expression which matches the same word 3 times, they are separated by a comma. For example, some inputs would be:
HEY,HEY,HEY - match
NO,NO,NO - match
HEY,HI,HEY - no match
HEY,H,Y - no match
HEY,NO,HEY - no match
How can I go about doing this? I've had a look at some example but they are only good for characters, not words.
Upvotes: 2
Views: 1320
Reputation: 939
This should do the trick:
^(\w+),\1,\1$
Explanation:
^
: beginning of the line. Needed to avoid matching "HHEY,HEY,HEY".
(\w+)
: matches one or more word characters. This is the first catpured group.
,
: the character comma.
\1
: a backreference to the first captured group. In the other words, matches whatever was matched in (\w+)
before.
,
: the character comma.
\1
: a backreference to the first captured group.
$
: end of the line. Needed to avoid matching "HEY,HEY,HEYY".
Source: https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx#Anchor_5
Example usage
static void Main()
{
var threeWords = new Regex(@"^(\w+),\1,\1$");
var lines = new[]
{
"HEY,HEY,HEY",
"NO,NO,NO",
"HEY,HI,HEY",
"HEY,H,Y",
"HEY,NO,HEY",
"HHEY,HEY,HEY",
"HEY,HEY,HEYY",
};
foreach (var line in lines)
{
var isMatch = threeWords.IsMatch(line) ? "" : "no ";
Console.WriteLine($"{line} - {isMatch}match");
}
}
Output:
HEY,HEY,HEY - match
NO,NO,NO - match
HEY,HI,HEY - no match
HEY,H,Y - no match
HEY,NO,HEY - no match
HHEY,HEY,HEY - no match
HEY,HEY,HEYY - no match
Upvotes: 8