仅当在 R 中的边界内时才替换特定字符

  • 本文关键字:替换 字符 边界 r regex
  • 更新时间 :
  • 英文 :


如何仅在特定字符伴有其他字符时才替换该字符而不消除后者?

例如

x <- "Elena has u$s 10,000. She's married.But she's not happy.Her husband's not happy either."
gsub("([a-z])\.+([A-Z])", ". ", x)
#[1] "Elena has u$s 10,000. She's marrie. ut she's not happ. er husband's not happy either."

这样我就失去了点之前的字符和点之后的字符

我希望我的结果如下:

#[1] "Elena has u$s 10,000. She's married. But she's not happy. Her husband's not happy either."

我们可以使用积极的前瞻正则表达式。

gsub("([a-z]\.)(?=[A-Z])", "\1 ", x, perl = TRUE)
#[1] "Elena has u$s 10,000. She's married. But she's not happy. Her husband's not happy either."

我们可以在不使用前瞻的情况下使用两个捕获组

gsub("([a-z]\.)([A-Z])", "\1 \2", x, perl = TRUE)
#[1] "Elena has u$s 10,000. She's married. But she's not happy. Her husband's not happy either."

最新更新