开发者

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 ands 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+)+".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜