Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 与R'的大小写匹配;什么是gsub?_Regex_R - Fatal编程技术网

Regex 与R'的大小写匹配;什么是gsub?

Regex 与R'的大小写匹配;什么是gsub?,regex,r,Regex,R,我试图在R中编写一个代码,将字符串中的所有“non”字更改为“none”,但我不想更改任何其他字,特别是我不想将“none”更改为“nonee” 我尝试了以下代码: gsub("non","none", "non none", fixed = TRUE) 但结果是: "none nonee" 用R的gsub做这件事有什么办法吗 您可以使用单词边界 x <- c("non" , "none") gsub( "\\bnon\\b" , "none" , x ) #[1] "none" "n

我试图在R中编写一个代码,将字符串中的所有“non”字更改为“none”,但我不想更改任何其他字,特别是我不想将“none”更改为“nonee”

我尝试了以下代码:

gsub("non","none", "non none", fixed = TRUE)
但结果是:

"none nonee"

用R的gsub做这件事有什么办法吗

您可以使用单词边界

x <- c("non" , "none")
gsub( "\\bnon\\b" , "none" , x )
#[1] "none" "none"

x x@Soroosh有什么问题吗?对于
“我想要的不是最好的”
这样的字符串,您认为会发生什么情况
^
$
分别锚定整个字符串的开头和结尾
\\b
只锚定bondary一词的两边。我想把它改为“我只想要最好的”——谢谢,我明白了