Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I would like to find same three word in a sentence
Eg: This is good good good time for for for all

I would like to find "good" and "for"

I used
C#
\b(\w+)\s+\1\b


but didn't get the result
Posted

Try this:
\b(\w+)\b\s+\1\b\s+\1\b

\1 means group 1 which refers to the (\w+)
effectively, the whole regex finds word that repeats thrice.
 
Share this answer
 
v5
 
Share this answer
 
That isn't really something a Regex is good at: it's a text processor, not a syntax analyser. It isn't good at comparing values to make sure they are the same.

Instead, do it in C# by using string.Split to break the sentence into individual words, and then either process them with a loop, or with Linq - it'll work a lot, lot better and be easier to read and modify later.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900