1

我正在尝试提取 url 中两个斜线之间的内容,为此我正在使用stringrfunction str_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]                                
[1,] "http://www.lefigaro.fr/flash-actu/"

但是,当我添加括号以提取字符串中的匹配项时,结果会意外更改:

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

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

必须是如何在正则表达式中解释括号的问题。有什么线索吗?

4

1 回答 1

1

也许如果你改变(.*?)([^/]*?)会起作用。

  • .匹配任何字符
  • [^/]匹配所有不是 a 的字符/

我不习惯 stringr,但这就是我在 php 中使用 preg_ 函数所做的事情。

希望能帮助到你

于 2014-11-27T14:31:59.870 回答