我想用基于字符串数字部分的href url替换任何具有issue # 000...
或issue #000...
(数字和磅号之间的注释空间)的内容。...
表示任意数目的数字。
##这里是一个MWE字符串:
News <- readLines(n=5)
CHANGES
* Fixed bug see GitHub issue #12
* Fixed bug see GitHub issue # 111. (John Doe)
News
##这里是href url的片段
## Roots
roota <- "<a href=\"https://github.com/trinker/qdap/issues/"
rootb <- "\">"
rootc <- "</a>"
##这里是所需的输出
c("CHANGES",
"",
"* Fixed bug see GitHub <a href=\"https://github.com/trinker/qdap/issues/12\">issue #12</a>" ,
"",
"* Fixed bug see GitHub <a href=\"https://github.com/trinker/qdap/issues/111\">issue #111</a>. (John Doe)"
)
##,这是我第一次尝试提取片段
gsub("(.)(issue)(.[#])(\\s*)([0-9]+)", "\\1", News)
##抓住数字,我几乎可以用把它们粘贴在一起
paste(roota, DIGIT_GRABBED, rootb, "issue #, DIGIT_GRABBED, rootc)
*I用regex标记标记,但请注意,R正则表达式是一个特定的品种,如果您回答,您应该熟悉R。。
发布于 2013-09-05 19:15:48
您可以简单地使用:
gsub(pattern="issue *# *([0-9]+)", replacement="<a href=\"https://github.com/trinker/qdap/issues/\\1\">issue #\\1</a>", x=News)
https://stackoverflow.com/questions/18644086
复制相似问题