R,gsub 在尝试获取超链接的子集时不起作用
R, gsub does not work when try to get subset of hyperlink
我试过 运行 代码如下。我想知道为什么 gsub 函数对这个输入不起作用。有人知道为什么以及如何处理这种情况吗?
> text
[1] <a href="https://itunes.apple.com/us/app/ubersocial-for-twitter/id396050119 mt=8&uo=4" rel="nofollow">UberSocial for Twitter on iOS</a>
65 Levels: <a href="http://aktualpost.com" rel="nofollow">Aktualpost</a> ...
> start = as.numeric(regexpr(">",text)[[1]])+1
> start
[1] 103
> to_cut = substr(text,1,start-1)
> to_cut
[1] "<a href=\"https://itunes.apple.com/us/app/ubersocial-for-twitter/id396050119?mt=8&uo=4\" rel=\"nofollow\">"
> new_text = gsub(to_cut,"",as.character(text))
> new_text
[1] "<a href=\"https://itunes.apple.com/us/app/ubersocial-for-twitter/id396050119?mt=8&uo=4\" rel=\"nofollow\">UberSocial for Twitter on iOS</a>"
在 "to_cut" 中有 ?
,在 "text" 中找不到。如果我们修复它,它应该可以工作,即检查 "to_cut" 中的 ?mt
和 "text" 中的 mt
。
gsub("^<a href=\"https://itunes.apple.com/us/app/ubersocial-for-twitter/id396050119 mt=8&uo=4\" rel=\"nofollow\">(.*)", "\1", text)
#[1] "UberSocial for Twitter on iOS</a>"
不清楚 OP 如何通过 ?
获得 "to_cut"
start = as.numeric(regexpr(">",text)[[1]])+1
to_cut <-substr(text,1,start-1)
to_cut
#[1] "<a href=\"https://itunes.apple.com/us/app/ubersocial-for-twitter/id396050119 mt=8&uo=4\" rel=\"nofollow\">"
gsub(to_cut, "", text)
#[1] "UberSocial for Twitter on iOS</a>"
我试过 运行 代码如下。我想知道为什么 gsub 函数对这个输入不起作用。有人知道为什么以及如何处理这种情况吗?
> text
[1] <a href="https://itunes.apple.com/us/app/ubersocial-for-twitter/id396050119 mt=8&uo=4" rel="nofollow">UberSocial for Twitter on iOS</a>
65 Levels: <a href="http://aktualpost.com" rel="nofollow">Aktualpost</a> ...
> start = as.numeric(regexpr(">",text)[[1]])+1
> start
[1] 103
> to_cut = substr(text,1,start-1)
> to_cut
[1] "<a href=\"https://itunes.apple.com/us/app/ubersocial-for-twitter/id396050119?mt=8&uo=4\" rel=\"nofollow\">"
> new_text = gsub(to_cut,"",as.character(text))
> new_text
[1] "<a href=\"https://itunes.apple.com/us/app/ubersocial-for-twitter/id396050119?mt=8&uo=4\" rel=\"nofollow\">UberSocial for Twitter on iOS</a>"
在 "to_cut" 中有 ?
,在 "text" 中找不到。如果我们修复它,它应该可以工作,即检查 "to_cut" 中的 ?mt
和 "text" 中的 mt
。
gsub("^<a href=\"https://itunes.apple.com/us/app/ubersocial-for-twitter/id396050119 mt=8&uo=4\" rel=\"nofollow\">(.*)", "\1", text)
#[1] "UberSocial for Twitter on iOS</a>"
不清楚 OP 如何通过 ?
start = as.numeric(regexpr(">",text)[[1]])+1
to_cut <-substr(text,1,start-1)
to_cut
#[1] "<a href=\"https://itunes.apple.com/us/app/ubersocial-for-twitter/id396050119 mt=8&uo=4\" rel=\"nofollow\">"
gsub(to_cut, "", text)
#[1] "UberSocial for Twitter on iOS</a>"