简体   繁体   中英

Finding adjacently repeated “and” in a sentence using regex?

I have a string " this is 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 ");

Something like this maybe: @"(\band\s+){2,}" (untested though). Or, since you're search/replacing anyway, @"(\band\s+)+" .

Regex.Replace(text, @"(\band\s+)+", "and ");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM