如何仅在特定字符伴有其他字符时才替换该字符而不消除后者?
例如
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."