R regex gsub单独的字母和数字

R regex gsub单独的字母和数字,regex,r,gsub,stringr,Regex,R,Gsub,Stringr,我有一个字母和数字混合的字符串: "The sample is 22mg" 我想拆分数字后面紧跟字母的字符串,如下所示: "The sample is 22 mg" 我试过这个: gsub('[0-9]+[[aA-zZ]]', '[0-9]+ [[aA-zZ]]', 'This is a test 22mg') 但是我没有得到想要的结果 有什么建议吗?您需要反向参考: test <- "The sample is 22mg" > gsub("([0-9])([a-zA-Z])"

我有一个字母和数字混合的字符串:

"The sample is 22mg"
我想拆分数字后面紧跟字母的字符串,如下所示:

"The sample is 22 mg"
我试过这个:

gsub('[0-9]+[[aA-zZ]]', '[0-9]+ [[aA-zZ]]', 'This is a test 22mg')
但是我没有得到想要的结果


有什么建议吗?

您需要反向参考:

test <- "The sample is 22mg"
> gsub("([0-9])([a-zA-Z])","\\1 \\2",test)
[1] "The sample is 22 mg"
测试gsub(([0-9])([a-zA-Z]),“\\1\\2”,测试) [1] “样本为22毫克”
括号中的任何内容都会被记住。然后它们被\1(对于parens中的第一个实体),\2等访问。第一个反斜杠在R中转义反斜杠的解释,以便传递给正则表达式解析器。

您需要在正则表达式中使用捕获括号,在替换中使用组引用。例如:

gsub('([0-9])([[:alpha:]])', '\\1 \\2', 'This is a test 22mg')
这里没有R-specific;
regex
gsub
的R帮助应该有一些用处