我寫了一個Ruby腳本來處理大量的文件,請使用以下URI從文檔的字符串表示提取的URI:紅寶石正則表達式掛
#Taken from: http://daringfireball.net/2010/07/improved_regex_for_matching_urls
URI_REGEX =/
( # Capture 1: entire matched URL
(?:
[a-z][\w-]+: # URL protocol and colon
(?:
\/{1,3} # 1-3 slashes
| # or
[a-z0-9%] # Single letter or digit or '%'
)
| # or
www\d{0,3}[.] # "www.", "www1.", "www2." … "www999."
| # or
[a-z0-9.\-]+[.][a-z]{2,4}\/ # looks like domain name followed by a slash
)
(?: # One or more:
[^\s()<>]+ # Run of non-space, non-()<>
| # or
\(([^\s()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels
)+
(?: # End with:
\(([^\s()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels
| # or
[^\s`!()\[\]{};:'".,<>?«»「」‘’] # not a space or one of these punct chars
)
)/xi
它工作得很好的所有文件的99.9%,但token = "synsem:local:cat:(subcat:SubMot,adjuncts:Adjs,subj:Subj),"
我使用的是標準的Ruby正則表達式oeprator:token =~ URI_REGEX
,我沒有得到任何異常或錯誤消息時遇到的文件中加入以下令牌總是掛斷我的腳本。
首先,我試圖解決封裝正則表達式評價爲Timeout::timeout
塊的問題,但是這會降低性能來得多。
有關如何解決此問題的任何其他想法?
請看,這正是我的意思。 +1使用正確的工具進行工作。 –