Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex gsub的逆_Regex_R - Fatal编程技术网

Regex gsub的逆

Regex gsub的逆,regex,r,Regex,R,我正在处理一些html代码。我想提取某些字符串 我想使用base R从字符串x中提取这个值:coleman\u 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&

我正在处理一些html代码。我想提取某些字符串

我想使用base R从字符串x中提取这个值:
coleman\u 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))

xgsub(“a>
(.+?)这将起作用,尽管它很难看

x<-"<code>(hi)<a href=\"Read\">auto</a></code>(coleman_l, SMOG4)<br />Read</li>"

x2 <- gsub("^.+(\\(.+\\)).+\\((.+)\\).+$","\\2",x)
x2
[1] "coleman_l, SMOG4"

x对于这些类型的问题,我会使用反向引用来提取我想要的部分

x <- 
  "<code>(hi)<a href=\"Read\">auto</a></code>(coleman_l, SMOG4)<br />Read</li>" 
gsub(".*a></code>(.+?)<br.*", "\\1", x)
# [1] "(coleman_l, SMOG4)"

FWIW,OP最初的方法本可以稍加调整就能奏效

> x
[1] "<code>(hi)<a href=\"Read\">auto</a></code>(coleman_l, SMOG4)<br />Read</li>"
> re <- "(?<=a></code>\\().*?(?=\\)<br)"
> regmatches(x, gregexpr(re, x, perl=TRUE))
[[1]]
[1] "coleman_l, SMOG4"
>x
[1] “
(嗨)
(科尔曼•l,烟雾4)
请阅读” >re regmatches(x,gregexpr(re,x,perl=TRUE)) [[1]] [1] “coleman_l,烟雾4”
与其他建议的解决方案相比,这种方法的一个优点是,如果存在多个匹配的可能性,那么所有匹配都将出现

> x <- '<code>(hi)<a href=\"Read\">auto</a></code>(coleman_l, SMOG4)<br />Read</li><code>(hi)<a href=\"Read\">auto</a></code>(coleman_l_2, SMOG4_2)<br />Read</li>'
> regmatches(x, gregexpr(re, x, perl=TRUE))
[[1]]
[1] "coleman_l, SMOG4"     "coleman_l_2, SMOG4_2"
>x regmatches(x,gregexpr(re,x,perl=TRUE))
[[1]]
[1] “科尔曼,烟雾4”“科尔曼,烟雾2”

str_extract
函数是否来自
stringr
帮助?@Ben我编辑说首选的base R,这样这个问题更适合未来的搜索者使用。请添加它作为解决方案。我知道你说的是base R,但是使用
XML
库和它的朋友
htmlTreeParse
xmlttreeparse
可能会更有用我们比使用正则表达式来处理html代码更合适。我仍然不清楚。是否类似于
gsub(.*a>
(.+?)@AnandaMahto perfect。请将其添加为解决方案。这是我的一次尝试,但我的
gsub
尝试偏离了方向。正则表达式通常漂亮吗?+1没有看到有用且漂亮的正则表达式:-)谢谢你的回复。+1谢谢你的回复。太好了。你不能把“re”改成
re吗?我发誓我试过了,但没用:P…修改我的解决方案。
> x
[1] "<code>(hi)<a href=\"Read\">auto</a></code>(coleman_l, SMOG4)<br />Read</li>"
> re <- "(?<=a></code>\\().*?(?=\\)<br)"
> regmatches(x, gregexpr(re, x, perl=TRUE))
[[1]]
[1] "coleman_l, SMOG4"
> x <- '<code>(hi)<a href=\"Read\">auto</a></code>(coleman_l, SMOG4)<br />Read</li><code>(hi)<a href=\"Read\">auto</a></code>(coleman_l_2, SMOG4_2)<br />Read</li>'
> regmatches(x, gregexpr(re, x, perl=TRUE))
[[1]]
[1] "coleman_l, SMOG4"     "coleman_l_2, SMOG4_2"