Finding adjacently repeated "and" in a sentence using regex?
I have a string "this is 开发者_StackOverflow社区a boy and and and and girl or biscut and pen" I would like to replace all these and
s by a single and
. Can this be done by using Regex?
I was using this regex but it fails:
@"\b(?<word>\w+)\s+(\k<word>)\b"
If you mean repeated words in general (as the regex in your post suggests):
resultString = Regex.Replace(subjectString, @"\b(\w+)(?:\s+\1)+\b", "$1");
Explanation:
\b # Assert start at a word boundary
(\w+) # Match a word
(?: # Try to match...
\s+ # Whitespace and
\1 # the same word as before
)+ # one or more times
\b # Assert end at a word boundary
If it's only and
you want to replace:
resultString = Regex.Replace(subjectString, @"\b(?:and\s+){2,}", "and ");
Regex.Replace(text, @"(\band\s+)+", "and ");
Something like this maybe: @"(\band\s+){2,}"
(untested though). Or, since you're search/replacing anyway, @"(\band\s+)+"
.
精彩评论