Reputation: 415
Let us say I have a string:
c <- "This is to inform you that you are being promoted because of good \r\nform form\r\n \r\nform\r\n recently"
What I want to do is to replace form with say "quality" to get the output as follows.
"This is to inform you that you are being promoted because of good \r\nquality quality\r\n \r\nquality\r\n recently"
Right now I am trying this:
gsub("form\\b|\\bform", "quality", c)
It gave output as follows.
"This is to inquality you that you are being promoted because of good \r\nquality quality\r\n \r\nquality\r\n recently"
So you see it also replaced "form" of "inform" too. This I do not want. So, now instead of detecting space on either side I want to detect special/formatting characters on either side of the string. I am not able to come up with the regex for this.
How do I do this?
PS: \r\n represent line breaks and new lines respectively.
Upvotes: 0
Views: 83
Reputation: 49097
The simple search expression \bform\b
works if the string on which it is applied contains carriage return and linefeed in the string as displayed below:
This is to inform you that you are being promoted because of good
form form
form
recently
But this expression would not work if the string on which the expression is applied contains literally "\r" and "\n" and the string value is therefore
This is to inform you that you are being promoted because of good \r\nform form\r\n \r\nform\r\n recently
For such a string the search expression (?:(?<=\\r|\\n)|\b)form\b
can be used to find the word form either after the string "\r" or the string "\n" (defined with \\r
respectively \\n
in an R string) or anywhere else in the text as entire word.
Upvotes: 1