R regex gsub separate letters and numbers

后端 未结 2 1932
孤街浪徒
孤街浪徒 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 15:03

    You need backreferencing:

    test <- "The sample is 22mg"
    > gsub("([0-9])([a-zA-Z])","\\1 \\2",test)
    [1] "The sample is 22 mg"
    

    Anything in parentheses gets remembered. Then they're accessed by \1 (for the first entity in parens), \2, etc. The first backslash escapes the backslash's interpretation in R so that it gets passed to the regular expression parser.

提交回复
热议问题