Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.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,考虑以下字符串:这是一个示例:这是另一个,这是另一个,等等。 我想替换:和,之间的所有空格字符。这是一个例子:{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])
    

    更新:有一种简单的方法可以使用
    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."
    

    原始答案(评分相当差)

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

    (?:\G(?!^)|:)[^,]*?\K\s(?=[^,]*,)
    
    替换为
    。请参阅

    详细信息

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


    re更新:有一种简单的方法可以使用
    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."
    

    原始答案(评分相当差)

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

    (?:\G(?!^)|:)[^,]*?\K\s(?=[^,]*,)
    
    替换为
    。请参阅

    详细信息

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


    re我认为非PCRE正则表达式在没有字符串函数或多个正则表达式的情况下无法做到这一点。但是,在单个正则表达式中,可以使用
    (?:(?
    是否应该存在?或者如果没有逗号,您是否还想替换字符串末尾的空格?@ctwheels您可以使用带有R
    gsub
    @WiktorStribiżew的PCRE正则表达式感谢您的确认,我们不确定R使用的是哪种味道。@ctwheels PCRE与base R一起使用,其他一些使用
    perl=TRUE
    参数,否则base R使用TRE正则表达式库,其他一些库使用Tcl正则表达式引擎。stringr*/*stringi使用ICU正则表达式库。这是正则表达式引擎的噩梦。我认为非PCRE正则表达式在没有字符串函数或多个正则表达式的情况下无法做到这一点。但是,在单个正则表达式中,您可以使用
    (?:(?
    是否应该存在?或者如果没有逗号,您是否还想替换字符串末尾的空格?@ctwheels您可以使用带有R
    gsub
    @WiktorStribiżew的PCRE正则表达式感谢您的确认,我们不确定R使用的是哪种味道。@ctwheels PCRE与base R一起使用,其他一些使用
    perl=TRUE
    参数,否则base R使用TRE正则表达式库,而其他一些库使用Tcl正则表达式引擎。stringr*/*stringi使用ICU正则表达式库。这是正则表达式引擎的噩梦。
    (?:\G(?!\a)|:)[^,]*?\K\h
    就足够了。@hwnd仅当要求在冒号后没有逗号时匹配字符串末尾的空格。
    (?:\G(?!\a)|:)[^,]*?\K\h
    仅当要求在冒号后没有逗号时匹配字符串末尾的空格时才足够。@hwnd。