2013-04-17 6 views
0

John Gruber의 Regex (http://daringfireball.net/2010/07/improved_regex_for_matching_urls)를 사용하여 텍스트 블록의 복잡한 URL과 일치 시키려합니다. 정규식은 매우 복잡합니다 (작업은 regex to find url in a text 참조).텍스트 블록 내의 복잡한 URL 일치 (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://%[email protected]%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:[email protected]?subject=TEST> (including brokets).", 
     "mailto:[email protected]", 
     "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([email protected](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) 

나는 당신의 도움을 주셔서 감사합니다 :

내 문제는 내가이 R 작동하지 않는다는 것입니다.

+1

'perl = TRUE'? – hadley

+0

적어도 문서에서'perl = TRUE'와 같은 옵션은 없습니다 (regexec는 이것을 가지고 있지 않은 유일한 명령입니다). 'regexpr'을 사용하고'perl = TRUE'를 설정하더라도 작동하지 않습니다. 지금까지 내가 알아 낸 한, Regex의 후반 부분 (''[^ \\ s'!() \\ [\\] {} ;: ' "., <>?«»" "' ']))')이 문제를 일으키는 것 같아요. – majom

+0

그러면 regmatches를 사용할 수 없으며'(? i)'는 영속 주의자입니다. – hadley

답변

1

이 경우 perl=TRUE을 지원하므로 gregexpr을 사용했습니다. 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)) 

다른 사람에게 도움이되기를 바랍니다.