More than 9 backreferences in gsub()

后端 未结 6 1616
温柔的废话
温柔的废话 2021-02-20 06:01

How to use gsub with more than 9 backreferences? I would expect the output in the example below to be \"e, g, i, j, o\".

> test <- \"abcdefghijklmnop\"
&g         


        
6条回答
  •  攒了一身酷
    2021-02-20 06:27

    The stri_replace_*_regex functions from the stringi package do not have such limitations:

    library("stringi")
    stri_replace_all_regex("abcdefghijkl", "(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)", "$10$1$11$12")
    ## [1] "jakl"
    

    If you want to follow the 1st capture group with 1, use e.g.

    stri_replace_all_regex("abcdefghijkl", "(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)", "$10$1$1\\1$12")
    ## [1] "jaa1l"
    

提交回复
热议问题