I have several character vectors like these in R:
a <- "text text NOTE 3/1"
b <- "text NOTE 4.3%"
All of them have a known word - NOTE - which is followed by a variate number of spaces and other characters.
What I want to do is to find the spaces between NOTE and other characters in string, and then replace each space with another character - say @
The desired output would be:
"text text NOTE@@@@@@3/1"
"text NOTE@@@4.3%"
So far I could only find the regular expression that will find NOTE and the spaces that follow it.
c <- gsub("NOTE\\s+", "@", a)
c
[1] "@3/1"
You can use
See the regex demo and the R demo.
Details:
(?:\G(?!^)|NOTE)- either the end of the previous successful match orNOTE\K- match reset operator that discards the text matched so far\s- a whitespace char.Here is a
stringrversion where the whitespaces matched afterNOTEare each replaced with a@char in thefunction(x) str_replace_all(x, "\\s", "@")callback function: