简体   繁体   中英

gsub replace word not followed by : with word: R

I thought this would be simpler, but I have strings not followed by ':', and strings with : inside the string. I want to append : to strings that don't end in : , and ignore strings that have : inside.

words
[1] "Bajos"    "Ascensor" "habs.:3"
gsub('\\b(?!:)', '\\1:', words, perl = TRUE)
[1] ":Bajos:"     ":Ascensor:"  ":habs:.::3:"
grep('\\W', words)
[1] 3
grep('\\w', words)
[1] 1 2 3 # ?

Desired output:

'Bajos:' 'Ascensor:' 'habs.:3'
sub("^([^:]*)$", "\\1:", words)
# [1] "Bajos:"    "Ascensor:" "habs.:3"   

or

nocolon <- !grepl(":", words)
words[nocolon] <- paste0(words[nocolon], ":")
words
# [1] "Bajos:"    "Ascensor:" "habs.:3"   

Use

"(\\p{L}+)\\b(?![\\p{P}\\p{S}])"

See regex proof .

EXPLANATION

--------------------------------------------------------------------------------
  (\p{L}+)                 one or more letters (group #1) 
--------------------------------------------------------------------------------
  \b                       word boundary
--------------------------------------------------------------------------------
  (?![\p{P}\p{S}])         no punctuation allowed on the right
--------------------------------------------------------------------------------

R code snippet :

gsub("(\\p{L}+)\\b(?![\\p{P}\\p{S}])", "\\1:", text, perl=TRUE)

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