如何检测r中字符两侧的特殊/格式化字符?

时间:2014-06-20 12:53:06

标签: regex r gsub

我们说我有一个字符串:

c <- "This is to inform you that you are being promoted because of good \r\nform form\r\n \r\nform\r\n recently"

我想要做的是用“质量”替换表单以获得如下输出。

"This is to inform you that you are being promoted because of good \r\nquality quality\r\n \r\nquality\r\n recently"

现在我正在尝试这个:

gsub("form\\b|\\bform", "quality", c)

它输出如下。

"This is to inquality you that you are being promoted because of good \r\nquality quality\r\n \r\nquality\r\n recently"

所以你看到它也取代了“通知”的“形式”。这个我不想要。因此,现在我不想检测任何一侧的空间,而是想检测字符串两侧的特殊/格式化字符。我无法为此提出正则表达式。

我该怎么做?

PS:\ r \ n分别表示换行符和换行符。

1 个答案:

答案 0 :(得分:1)

如果应用它的字符串在字符串中包含回车符和换行符,则简单搜索表达式\bform\b有效,如下所示:

This is to inform you that you are being promoted because of good 
form form

form
 recently

但是如果应用表达式的字符串字面上包含“\ r”和“\ n”并且字符串值是

,则此表达式将不起作用
This is to inform you that you are being promoted because of good \r\nform form\r\n \r\nform\r\n recently

对于这样的字符串,搜索表达式(?:(?<=\\r|\\n)|\b)form\b可用于在字符串“\ r \ n”或字符串“\”之后找到 表单 n“(在R字符串中分别用\\r \\n定义)或在文本中的任何其他位置定义为整个单词。