整个字符串考虑以下字符串:匹配R中
string = "I have #1 file and #11 folders"
我想用这个词one
更换模式#1
,但我不希望修改日#11
。结果应该是:
string = "I have one file and #11 folders"
我曾尝试:
string = gsub("#1", "one, string, fixed = TRUE)
但是这取代了#1和#11。我也尝试过:
string = gsub("^#1$", "one, string, fixed = TRUE)
但是这不能代替任何东西,因为模式是包含空格的字符串的一部分。
请注意,如果初始字符串看起来像:
string = "I have #1 file blah blah blah and #11 folders"
我希望得到的结果是:
string = "I have 1 file blah blah blah and #11 folders"
换句话说,我真的只是想改变精确模式#1
而不触及琴弦的其余部分。那可能吗?
这听起来像在'pattern'参数中使用'[:alpha:]'类,没有'perl = TRUE',是的? – TheComeOnMan
@Codoremifa,'\ w'类似于'[:alpha:]',但我的意思是(现在已经修复)是'\ b'(都需要一个额外的'\'来转义另一个) “字”字和“非字”字之间没有任何内容,在这种情况下,“1”和“”之间没有任何内容匹配,但不包含“1”和“1”之间的内容。 –