简体   繁体   中英

regex for repeating word to repeating two words

I am trying to write some regex pattern that will look through a sentence and remove any one or two sequentially repeated words

for example:

# R code below
string_a = "hello hello, how are you you?"
string_b = "goodbye world goodbye world, I am flying to the the moon!"

gsub(pattern, "", string_a)
gsub(pattern, "", string_b)

Desired outputs are

[1] "hello, how are you?"
[2] "goodbye world, I am flying to the moon!"

Try

 gsub("(\\S+(\\s+\\S+)?)\\s+\\1+", "\\1", c(string_a, string_b))

-output

[1] "hello, how are you?"                  
[2] "goodbye world, I am flying to the moon!"

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