R regex gsub separate letters and numbers

后端 未结 2 1941
孤街浪徒
孤街浪徒 2021-02-15 14:08

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

2条回答
  •  佛祖请我去吃肉
    2021-02-15 14:59

    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')
    

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

提交回复
热议问题