簡體   English   中英

正則表達式:替換兩個字符之間的所有空格

[英]Regex: Replacing all spaces between two characters

考慮以下字符串: 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.字符串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

更新:有一種簡單的方法可以使用stringr::str_replace_all使用匿名函數作為替換參數來替換 R 中任意字符串之間的任何內容:

通用stringr方法

library(stringr)

# left - left boundary
# right - right boundary
# x - input
# what - regex pattern to search for inside matches
# repl - replacement text for the in-pattern matches
ReplacePatternBetweenTwoStrings <- function(left, right, x, what, repl) {
  left  <- gsub("([][{}()+*^${|\\\\?.])", "\\\\\\1", left)
  right <- gsub("([][{}()+*^${|\\\\?.])", "\\\\\\1", right)
  str_replace_all(x, 
     paste0("(?s)(?<=", left, ").*?(?=", right, ")"),
     function(z) gsub(what, repl, z)
  )
}

x <- "This is an example: this is another one, and this is yet another, and other, and so on."
ReplacePatternBetweenTwoStrings(":", ",", x, "\\s+", "_")
## => [1] "This is an example:_this_is_another_one, and this is yet another, and other, and so on."

請參閱此 R 演示

替換最近的:和 之間的所有空格,

這是上面的一個簡單的邊緣情況,當:[^:,]+,匹配 a : ,然后是除:, (分隔符字符)之外的任意數量的字符,然后是 a , ,然后空格被替換為下划線僅比賽:

stringr::str_replace_all(x, ":[^:,]+,", function(z) gsub("\\s+", "_", z))

查看正則表達式演示

原始答案(比例相當差)

您可以使用以下正則表達式:

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

替換為_ 請參閱正則表達式演示

詳情

  • (?:\\G(?!^)|:) - 前一個匹配的結尾( \\G(?!)^ )或冒號
  • [^,]*? - 除 之外的任何 0+ 個字符,盡可能少
  • \\K - 匹配重置運算符丟棄到目前為止匹配的文本
  • \\s - 一個空格
  • (?=[^,]*,) - 一個積極的前瞻檢查,確保在零個或多個除逗號之外的字符之后有一個,

R演示

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

這是一個略顯粗糙的答案:

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])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM