Home > Net >  Is there a way in R to replace a pattern only if it is preceded by letter?
Is there a way in R to replace a pattern only if it is preceded by letter?

Time:07-23

I have a string (more precisely a query) where I need to add some characters ("\n") at the end of all words between ''. The problem is that my string algo contains numbers between '', and they should remain unchanged. A simplified version of the beginning of the string follows:

"SELECT CASE WHEN V1 = '1' THEN 'word' WHEN V1 = '3' THEN 'another word' END"

And the desired result with the replacements would be:

"SELECT CASE WHEN V1 = '1' THEN 'word\n' WHEN V1 = '3' THEN 'another word\n' END"

I tried using gsub function, but I couldn`t figure out how to make it work the way I need. If I use:

gsub("' ", "\n' ", string)

The numbers are also replaced. Or if I use:

gsub("[a-z]' ", "\n' ", string)

I end up removing the last letter of the words.

Am I missing an obvious solution here? I`m quite new at programming, so I apreciate some help if possible.

CodePudding user response:

Capture as group ((...)) for the letter followed by the ' and then use the backreference (\\1) of the group captured in the replacement. In the OP's post, the letter was matched, but it was not captured and thus got removed in the output

gsub("([A-Za-z])' ", "\\1\n' ", string)
[1] "SELECT CASE WHEN V1 = '1' THEN 'word\n' WHEN V1 = '3' THEN 'another word\n' END"

data

string <- "SELECT CASE WHEN V1 = '1' THEN 'word' WHEN V1 = '3' THEN 'another word' END"
  • Related