antoine-sac antoine-sac - 3 months ago 19
R Question

using captured groups in str_replace / stri_replace - stringi vs stringr

Most

stringr
functions are just wrappers around corresponding
stringi
functions.
str_replace_all
is one of those. Yet my code does not work with
stri_replace_all
, the corresponding
stringi
function.

I am writing a quick regex to convert (a subset of) camel case to spaced words.

I am quite puzzled as to why this works:

str <- "thisIsCamelCase aintIt"
stringr::str_replace_all(str,
pattern="(?<=[a-z])([A-Z])",
replacement=" \\1")
# "this Is Camel Case ain't It"


And this does not:

stri_replace_all(str,
regex="(?<=[a-z])([A-Z])",
replacement=" \\1")
# "this 1s 1amel 1ase ain't 1t"

Answer

If you look at the source for stringr::str_replace_all you'll see that it calls fix_replacement(replacement) to convert the \\# capture group references to $#. But the help on stringi:: stri_replace_all also clearly shows that you use $1, $2, etc for the capture groups.

str <- "thisIsCamelCase aintIt"
stri_replace_all(str, regex="(?<=[a-z])([A-Z])", replacement=" $1")
## [1] "this Is Camel Case aint It"
Comments