R regex gsub分隔字母和数字 [英] R regex gsub separate letters and numbers

查看:150
本文介绍了R regex gsub分隔字母和数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含字母和数字的字符串:

I have a string that's mixed letters and numbers:

"The sample is 22mg"

我想在数字后面紧跟一个字母的情况下拆分字符串:

I'd like to split strings where a number is immediately followed by letter like this:

"The sample is 22 mg"

我已经尝试过了:

gsub('[0-9]+[[aA-zZ]]', '[0-9]+ [[aA-zZ]]', 'This is a test 22mg')

但没有得到想要的结果.

but am not getting the desired results.

有什么建议吗?

推荐答案

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

You need to use capturing parentheses in the regular expression and group references in the replacement. For example:

gsub('([0-9])([[:alpha:]])', '\\1 \\2', 'This is a test 22mg')

这里没有R特有的; regexgsub的R帮助应该有用.

There's nothing R-specific here; the R help for regex and gsub should be of some use.

这篇关于R regex gsub分隔字母和数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆