2009年11月28日 星期六

Daring Fireball: A Liberal, Accurate Regex Pattern for Matching URLs

這樣就不用每次再去寫一個判斷某字串是不是 URL 的 method/function 了

A common problem programming problem: identify the URLs in an arbitrary string of text, where by “arbitrary” let’s agree we mean something unstructured such as an email message or a tweet. I offer a solution, in the form of the following regex pattern:

\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))

[From Daring Fireball: A Liberal, Accurate Regex Pattern for Matching URLs]

沒有留言:

張貼留言