【问题标题】:Matching complex URLs within text blocks (R)匹配文本块中的复杂 URL (R)
【发布时间】:2013-04-17 11:41:41
【问题描述】:

我想使用 John Gruber (http://daringfireball.net/2010/07/improved_regex_for_matching_urls) 的 Regex 来匹配文本块中的复杂 URL。正则表达式非常复杂(任务也是如此,请参阅regex to find url in a text)。

我的问题是我无法使用 R:

x <-   c("http://foo.com/blah_blah",
        "http://foo.com/blah_blah/",
        "(Something like http://foo.com/blah_blah)",
        "http://foo.com/blah_blah_(wikipedia)",
         "http://foo.com/more_(than)_one_(parens)",
         "(Something like http://foo.com/blah_blah_(wikipedia))",
         "http://foo.com/blah_(wikipedia)#cite-1",
         "http://foo.com/blah_(wikipedia)_blah#cite-1",
         "http://foo.com/unicode_(✪)_in_parens",
         "http://foo.com/(something)?after=parens",
         "http://foo.com/blah_blah.",
         "http://foo.com/blah_blah/.",
         "<http://foo.com/blah_blah>",
         "<http://foo.com/blah_blah/>",
         "http://foo.com/blah_blah,",
         "http://www.extinguishedscholar.com/wpglob/?p=364.",
         "http://✪df.ws/1234",
         "rdar://1234",
         "rdar:/1234",
         "x-yojimbo-item://6303E4C1-6A6E-45A6-AB9D-3A908F59AE0E",
         "message://%3c330e7f840905021726r6a4ba78dkf1fd71420c1bf6ff@mail.gmail.com%3e",
         "http://➡.ws/䨹",
         "www.c.ws/䨹",
         "<tag>http://example.com</tag>",
         "Just a www.example.com link.",
         "http://example.com/something?with,commas,in,url, but not at end",
         "What about <mailto:gruber@daringfireball.net?subject=TEST> (including brokets).",
         "mailto:name@example.com",
         "bit.ly/foo",
         "“is.gd/foo/”",
         "WWW.EXAMPLE.COM",
         "http://www.asianewsphoto.com/(S(neugxif4twuizg551ywh3f55))/Web_ENG/View_DetailPhoto.aspx?PicId=752",
         "http://www.asianewsphoto.com/(S(neugxif4twuizg551ywh3f55))",
         "http://lcweb2.loc.gov/cgi-bin/query/h?pp/horyd:@field(NUMBER+@band(thc+5a46634))")


t <- regexec("\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'".,<>?«»“”‘’]))", x)             

regmatches(x,t)

感谢您的帮助。

【问题讨论】:

  • 文档中至少没有perl=TRUE 这样的选项(regexec 是唯一没有这个的命令)。即使我使用regexpr 并设置perl=TRUE 它也不起作用。据我所知,正则表达式的后半部分 (|[^\\s!()\[\]{};:'".,?«»“”'']))`) 似乎导致问题。
  • 那么,你不能使用正则匹配,而且我很确定(?i) 是一个perlism。
  • 你对(?i) 是对的,R 确实不需要这个。我在我的问题中改变了它。但是,如果我删除上面提到的部分,该命令运行良好(但它不能识别上面列出的所有示例性 URL)。

标签: regex r


【解决方案1】:

我最终使用了gregexpr,因为它支持perl=TRUE。在为 R 调整 Regex 之后,我想出了以下解决方案(使用上面的数据)。

findURL <- function(x){
  t <- gregexpr("(?xi)\\b(
             (?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)
             (?:[^\\s\\(\\)<>]+|\\(([^\\s\\(\\)<>]+|(\\([^\\s\\(\\)<>]+\\)))*\\))+
             (?:\\(([^\\s\\(\\)<>]+|(\\([^\\s\\(\\)<>]+\\)))*\\)|[^\\s`!\\(\\)\\[\\]{};:'\\\"\\.,<>\\?«»“”‘’])
             )",x, perl=TRUE, fixed=FALSE)
  regmatches(x,t)
} 

# Find URLs
urls <- findURL(x) 

# Count URLs
count.urls.temp <- lapply(urls, length)    
count.urls <- sum(unlist(count.urls.temp))

我希望这对其他人有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 2018-11-10
    • 2018-01-06
    • 1970-01-01
    • 2019-10-07
    • 2020-01-25
    相关资源
    最近更新 更多