【问题标题】:Replace string content with arbitrary number of four characters in middle of string?用字符串中间的任意数量的四个字符替换字符串内容?
【发布时间】:2015-07-24 17:39:45
【问题描述】:

目标:把x变成y;其中 x 具有任意数量的空格、\rs 和 \ns。

x <- "some text,                    \r\n                    \r\n)more text"
y <- "some text)more text"

我使用 str_replace_all() 做了一些尝试:

str_replace_all(x, "[,][ \r\n][)]", "")
str_replace_all(x, ",[ \r\n])", "")

【问题讨论】:

  • 任意字符数?你的意思是[add chars here]+ 吗?或者,像,[ \r\n]*(?=\))

标签: regex r string text stringr


【解决方案1】:

gsub 将为您完成这项工作。

gsub(",\\s*\\n\\s*\\)", ")", s)

gsub(",\\s*[\\r\\n]+\\s*\\)", ")", s)

示例:

> x <- "some text,                    \r\n                    \r\n)more text"
> gsub(",\\s*\\n\\s*\\)", ")", x)
[1] "some text)more text"

【讨论】:

    猜你喜欢
    • 2018-11-15
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多