R 正则表达式:替换两个字符之间的所有空格
考虑以下字符串:R 正则表达式:替换两个字符之间的所有空格,r,regex,R,Regex,考虑以下字符串:这是一个示例:这是另一个,这是另一个,等等。 我想替换:和,之间的所有空格字符。这是一个例子:{u这是另一个,这是另一个,等等。 到目前为止,我所尝试的: (?以下是一个略显粗俗的答案: txt="This is an example: this is another one, and this is yet" split_str=unlist(strsplit(gsub("^(.*:)(.*)(,.*)", "\\1$\\2$\\3", txt), split="$", f
这是一个示例:这是另一个,这是另一个,等等。
我想替换:
和,
之间的所有空格字符。这是一个例子:{u这是另一个,这是另一个,等等。
到目前为止,我所尝试的:
(?以下是一个略显粗俗的答案:
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])
下面是一个略显粗俗的答案: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])
更新:有一种简单的方法可以使用
使用匿名函数作为替换参数替换R中任意字符串之间的任何内容: 通用stringr::str_replace_all
方法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."
替换为(?:\G(?!^)|:)[^,]*?\K\s(?=[^,]*,)
。请参阅 详细信息。
-上一个匹配((?:\G(?)|:)
)或冒号的结束\G(?!)^
-除[^,]*?
以外的任何0+字符尽可能少,
-匹配重置运算符放弃目前匹配的文本\K
-空白\s
-一种积极的前瞻性检查,确保除逗号以外的零个或多个字符后面有一个(?=[^,]*,)
,
re更新:有一种简单的方法可以使用
使用匿名函数作为替换参数来替换R中任意字符串之间的任何内容: 通用stringr::str_replace_all
方法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."
替换为(?:\G(?!^)|:)[^,]*?\K\s(?=[^,]*,)
。请参阅 详细信息。
-上一个匹配((?:\G(?)|:)
)或冒号的结束\G(?!)^
-除[^,]*?
以外的任何0+字符尽可能少,
-匹配重置运算符放弃目前匹配的文本\K
-空白\s
-一种积极的前瞻性检查,确保除逗号以外的零个或多个字符后面有一个(?=[^,]*,)
,
re我认为非PCRE正则表达式在没有字符串函数或多个正则表达式的情况下无法做到这一点。但是,在单个正则表达式中,可以使用
(?:(?
是否应该存在?或者如果没有逗号,您是否还想替换字符串末尾的空格?@ctwheels您可以使用带有R,
@WiktorStribiżew的PCRE正则表达式感谢您的确认,我们不确定R使用的是哪种味道。@ctwheels PCRE与base R一起使用,其他一些使用gsub
参数,否则base R使用TRE正则表达式库,其他一些库使用Tcl正则表达式引擎。stringr*/*stringi使用ICU正则表达式库。这是正则表达式引擎的噩梦。我认为非PCRE正则表达式在没有字符串函数或多个正则表达式的情况下无法做到这一点。但是,在单个正则表达式中,您可以使用perl=TRUE
(?:(?
是否应该存在?或者如果没有逗号,您是否还想替换字符串末尾的空格?@ctwheels您可以使用带有R,
@WiktorStribiżew的PCRE正则表达式感谢您的确认,我们不确定R使用的是哪种味道。@ctwheels PCRE与base R一起使用,其他一些使用gsub
参数,否则base R使用TRE正则表达式库,而其他一些库使用Tcl正则表达式引擎。stringr*/*stringi使用ICU正则表达式库。这是正则表达式引擎的噩梦。perl=TRUE
就足够了。@hwnd仅当要求在冒号后没有逗号时匹配字符串末尾的空格。(?:\G(?!\a)|:)[^,]*?\K\h
仅当要求在冒号后没有逗号时匹配字符串末尾的空格时才足够。@hwnd。(?:\G(?!\a)|:)[^,]*?\K\h