Home > database >  gsub replace word not followed by : with word: R
gsub replace word not followed by : with word: R

Time:09-10

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'

CodePudding user response:

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

or

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

CodePudding user response:

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)
  • Related