More than 9 backreferences in gsub()

后端 未结 6 1614
温柔的废话
温柔的废话 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:13

    This limitation to 9 backreferences is specific to the sub() and gsub()functions, not to functions like grep() and the like. Support for more than 9 backreferences in R implies using PCRE regular expression (i.e. the perl=TRUE argument); however, even with this option, the sub() and gsub() functions do not support it.

    The R documentation is explicit on this point: see ?regexp

    There can be more than 9 backreferences (but the replacement in sub can
    only refer to the first 9).
    

    Furthermore the idea of using named capture groups to circumvent this limitation is bound to fail since named capture groups are not supported with sub() functions.

    regexpr and gregexpr support ‘named capture’. If groups are named,
    e.g., "(?[A-Z][a-z]+)" then the positions of the matches are also
    returned by name. (Named backreferences are not supported by sub.)
    

提交回复
热议问题