Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅当在R中的边界内时替换特定字符_R_Regex - Fatal编程技术网

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

仅当在R中的边界内时替换特定字符,r,regex,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 h

如何在不删除其他字符的情况下,只替换与其他字符相伴的特定字符

比如说

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."

我们可以使用正向前瞻正则表达式

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."

请解释您正在尝试做什么,以及您的预期输出是什么?我想用一个没有空格的点来分隔粘在一起的句子,用一个有空格的点来分隔,同时不丢失分隔点的字符。我希望我的结果如下:#[1]“埃琳娜有一万美元。她结婚了。但她并不快乐。她的丈夫也不高兴。“它和你在
x
中的东西有何不同?”非常完美。谢谢Ronak!
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."