用开头的模式替换字符串末尾的模式

Replace pattern at end of a string by pattern at the beginning

我对正则表达式还很陌生,如果这是一个明显愚蠢的问题,我深表歉意。

假设我有一个形式为 (c"fair*", "beaut*") 的字符串,我想用开头的脱字符号 (^) 替换末尾的星号 (*):c( “^公平”,“^美丽”)。我怎样才能使用 stringr 来做到这一点?我看过this excellent introduction to regexp,但没弄明白。我用 stringr::str_replace_all() 做了几次尝试,但都无济于事。问题似乎是 stringr::str_replace_all() 中的替换不能是正则表达式。是这样吗?如果是,还有其他方法吗?

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^(*)"

reprex package (v2.0.1)

于 2021-09-15 创建

您可以使用

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

regex demo and the R demo online。请注意 str_replace 就足够了,因为这里只能进行一次替换。这就是为什么我也建议 sub 而不是 gsub 作为基础 R 对应物。

(.*)\*$ 模式尽可能多地匹配任何零个或多个字符(换行符除外)并将其捕获到组 1 中,然后仅匹配文字 * 星号字符(\*) 在字符串末尾 ($).

^ 替换模式将匹配项替换为 ^ 和第 1 组的值。

注意:如果您的字符串包含换行符,stringr 解决方案将需要稍作修改:str_replace(x, "(?s)(.*)\*$", "^\1").

这个也可以帮助:

sub("(^[[:alpha:]]+)\*$", "^\1", x, perl = TRUE)

[1] "^fair"  "^beaut"