我有一些HTML代码需要处理。我想要提取特定的字符串。
我想要从字符串x中使用基本R来提取以下内容:coleman_l, SMOG4
这是我的代码:
x <- "<code>(hi)<a href=\"Read\">auto</a></code>(coleman_l, SMOG4)<br />Read</li>"
#remove the string (this works)
gsub("a></code>(.+?)<br", "a></code><br", x)
#> gsub("a></code>(.+?)<br", "a></code><br", x)
#[1] "<code>(hi)<a href=\"Read\">auto</a></code><br />Read</li>"
#attempt to extract that information (doesn't work)
re <- "(?<=a></code>().*?(?=)<br)"
regmatches(x, gregexpr(re, x, perl=TRUE))
错误信息:
> regmatches(x, gregexpr(re, x, perl=TRUE))
Error in gregexpr(re, x, perl = TRUE) :
invalid regular expression '(?<=a></code>().*?(?=)<br)'
In addition: Warning message:
In gregexpr(re, x, perl = TRUE) : PCRE pattern compilation error
'lookbehind assertion is not fixed length'
at ')'
enter code here
注意:标记为正则表达式,但这是 R 特定的正则表达式。
stringr
中的str_extract
函数是否有帮助? - Ben BolkerXML
库及其相关的htmlTreeParse
或xmlTreeParse
比使用正则表达式处理 HTML 代码更合适。 - thelatemailgsub(".*a></code>(.+?)<br.*", "\\1", x)
这样的东西是你要找的吗? - A5C1D2H2I1M1N2O1R2T1gsub
尝试完全偏离了轨道。 - Tyler Rinker