regex - 删除转义\n

标签 regex r

一些数据:

x <- c("Town\<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b2522252d240b25243f3e3865282426" rel="noreferrer noopener nofollow">[email protected]</a>\n\n","\<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="96f8fff8f0f9d6f8f9e2e3e5b8f5f9fb" rel="noreferrer noopener nofollow">[email protected]</a>\n","\<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d1314131b123d131209080e531e1210" rel="noreferrer noopener nofollow">[email protected]</a>\n\n","\<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0ded9ded6dff0dedfc4c5c39ed3dfdd" rel="noreferrer noopener nofollow">[email protected]</a>\n","<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4cdcac2cbe4cacbd0d1d78ac7cbc9" rel="noreferrer noopener nofollow">[email protected]</a>")

这可行,但\n之前有单词

gsub("\n","",x,fixed=T)

这有效,但如果没有 \n 则给出 NA

unlist(lapply(regmatches(x,gregexpr("(?<=\n).*",x,perl=TRUE)), function(x) x[1]))

编辑:期望的结果:

rep("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a33343c351a34352e2f2974393537" rel="noreferrer noopener nofollow">[email protected]</a>",5)

最佳答案

使用 gsub。

gsub("(?s)^.*?\\n|\\n.*", "", x, perl=T)

关于regex - 删除转义\n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30841769/

相关文章:

java 正则表达式 : replace ${var}

java - 正则表达式按分隔符分割,排除转义符并保留空字符串

r - 为什么 Rtools 3.1 在 Windows 上不支持 C++11

r - ggplot2无法绘制曲线

regex - Git 忽略 : How to match one or more digits

c++ - 词法分析 gawk 代码时区分除法与正则表达式

c# - 在保留 "splitter words"的替代完整单词列表上分割句子

r - 在 R 中循环列表时计算年度性能统计数据

r - 如何连续重启/循环R脚本

r - R 中无向图中的循环