I want to use the $0 regex operator via the str_replace() function (stringr package).
For instance, I have a vector:
test <- c("1111", "2222", "3333")
test
[1] "1111" "2222" "3333"
and I want to get the next vector: "111_1" "222_2" "333_3"
When I try to do it via str_replace, I get the following:
str_replace(test, "^\\d{3}", "\\$0_")
[1] "\\111_1" "\\222_2" "\\333_3"
How does $0 work in stringr?
P.S.: I don't want to use (?<=) and (?=) bulky operators.
CodePudding user response:
It seems that \0 with no $ refers to the match so:
str_replace(test, "^\\d{3}", "\\0_")
## [1] "111_1" "222_2" "333_3"
In base R it could be done like this where \1 refers to the first (and here only) capture group:
sub("^(\\d{3})", "\\1_", test)
## [1] "111_1" "222_2" "333_3"