Regex str_match中的括号会更改匹配

Regex str_match中的括号会更改匹配,regex,r,stringr,Regex,R,Stringr,我试图提取url中两个斜杠之间的内容,为此我使用stringr函数stru match library(stringr) test <- "http://www.lefigaro.fr/flash-actu/2014/04/08/97001-20140408FILWWW00162-ump-cope-defend-sa-gestion-financiere.php" 但是,当我添加括号以提取字符串中的匹配项时,结果会意外更改: str_match(test, "http://.*?/(.*

我试图提取url中两个斜杠之间的内容,为此我使用
stringr
函数
stru match

library(stringr)
test <- "http://www.lefigaro.fr/flash-actu/2014/04/08/97001-20140408FILWWW00162-ump-cope-defend-sa-gestion-financiere.php"
但是,当我添加括号以提取字符串中的匹配项时,结果会意外更改:

str_match(test, "http://.*?/(.*?)/")

     [,1]                                      [,2]  
[1,] "http://www.lefigaro.fr/flash-actu/2014/" "2014"
必须考虑如何在正则表达式中解释括号。有什么线索吗?

如果你用
([^/]*?)
更改
(.*?
),可能会有用

  • 匹配任何字符
  • [^/]
    匹配所有非
    /
我不习惯stringr,但这就是我在php中使用preg_uu函数所做的


希望对您有所帮助。

stringr库似乎有问题。谢谢Heru Luin,您的建议适用于这种特殊情况:
str_match(test,“http://[^/]*?/(.*?/”)
输出“flash actu”。但是,我不理解错误的原因,如果它确实是一个错误,那么就在捕获组中。您的正则表达式还捕获了斜杠“/”,因此它匹配了“flash actu”、斜杠和更多位。添加“[^/]”将禁止正则表达式匹配斜杠,因此现在只需要flash actu:)
str_match(test, "http://.*?/(.*?)/")

     [,1]                                      [,2]  
[1,] "http://www.lefigaro.fr/flash-actu/2014/" "2014"