正则表达式:替换两个字符之间的所有空格

时间:2017-11-07 17:28:15

标签: r regex

考虑以下字符串:This is an example: this is another one, and this is yet another, and other, and so on. 我想替换:,之间的所有空格字符。所以它看起来像This is an example:_this_is_another_one, and this is yet another, and other, and so on.

到目前为止我已尝试过:

  • (?<=:)\s+(?=[^,]*,)(仅匹配第一个空格)
  • :\s+(?=[^:,]*,)(与上述相同)
  • \s+(?=[^:,]*,)(匹配This is an example:_this_is_another_one,_and_this_is_yet_another,_and_other, and so on

2 个答案:

答案 0 :(得分:3)

您可以使用以下正则表达式:

(?:\G(?!^)|:)[^,]*?\K\s(?=[^,]*,)

替换为_。请参阅regex demo

<强>详情

  • (?:\G(?!^)|:) - 上一场比赛(\G(?!)^)或冒号
  • 的结尾
  • [^,]*? - 尽可能少的,以外的任何0 +字符
  • \K - 匹配重置运算符,丢弃目前为止匹配的文本
  • \s - 空白
  • (?=[^,]*,) - 一个积极的超前检查,确保在逗号以外的零个或多个字符后面有,

R demo

re <- "(?:\\G(?!^)|:)[^,]*?\\K\\s(?=[^,]*,)"
x <- "This is an example: this is another one, and this is yet another, and other, and so on."
gsub(re, "_", x, perl=TRUE)
# => [1] "This is an example:_this_is_another_one, and this is yet another, and other, and so on."

答案 1 :(得分:0)

这是一个稍微粗略的回答:

txt="This is an example: this is another one, and this is yet"

split_str=unlist(strsplit(gsub("^(.*:)(.*)(,.*)", "\\1$\\2$\\3", txt), split="$", fixed=T))

paste0(split_str[1], gsub(" ", "_",split_str[2]), split_str[3])