Home > Mobile >  Replace pattern at end of a string by pattern at the beginning
Replace pattern at end of a string by pattern at the beginning

Time:09-16

I'm very new to regular expressions, so my apologies if this is an obviously stupid question.

Suppose I have a string of the form (c"fair*", "beaut*") and I want to replace the asterisk (*) at the end by a caret (^) at the beginning: c("^fair", "^beaut"). How could I go about doing that using stringr? I have read this excellent introduction to regexp, but did not figure it out. I made several attempts with stringr::str_replace_all(), to no avail. The issue seems to be that the replacement in stringr::str_replace_all() can't be a regexp. Is that so? If yes, is there any other way to do this?

library(stringr)

x <- c("fair*", "beaut*")
str_replace_all(x, "\\*", "^\\^")
#> [1] "fair^^"  "beaut^^"
str_replace_all(x, "\\*", "^\\^")
#> [1] "fair^^"  "beaut^^"
str_replace_all(x, "\\*", "\\^*")
#> [1] "fair^*"  "beaut^*"
str_replace_all(x, "\\*", "\\^(*)")
#> [1] "fair^(*)"  "beaut^(*)"

Created on 2021-09-15 by the reprex package (v2.0.1)

CodePudding user response:

You can use

library(stringr)
x <- c("fair*", "beaut*")
str_replace(x, "(.*)\\*$", "^\\1")
# => [1] "^fair"  "^beaut"
sub("(.*)\\*$", "^\\1", x)
# => [1] "^fair"  "^beaut"

See the regex demo and the R demo online. Note that str_replace will be enough as there is only one substitution that can occur here. That is why I also suggest sub rather than gsub as a base R counterpart.

The (.*)\*$ pattern matches and captures into Group 1 any zero or more chars (other than line break chars) as many as possible, then just matches a literal * asterisk char (\*) at the end of string ($).

The ^\1 replacement pattern replaces the match with ^ and the value of Group 1.

NOTE: If your strings contain line breaks, the stringr solution will need a slight fix: str_replace(x, "(?s)(.*)\\*$", "^\\1").

  • Related