[英]Matching dashes in a URL regex
我使用以下正則表達式從文本中獲取網址(例如"this is text http://url.com/blabla possibly some more text"
)。
'@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@'
這適用於所有URL,但我發現它不適用於縮短的URL,例如: "blabla bla http://ff.im/-bEnA blabla"
在比賽后變為http://ff.im/
。
我懷疑它做的破折號-
斜線后/
。
簡短答案: [\\w/_\\.]
不匹配-
因此將其設置為[-\\w/_\\.]
長答案:
@ - delimiter
( - start of group
https?:// - http:// or https://
([-\w.]+)+ - capture 1 or more hyphens, word characters or dots, 1 or more times.. this seems odd - don't know what the second + is for
(:\d+)? - optionally capture a : and some numbers (the port)
( - start of group
/ - leading slash
( - start of group
[\w/_\.] - any word character, underscore or dot - you need to add hyphen to this list or just make it [^?\S] - any char except ? or whitespace (the path + filename)
(\?\S+)? - optionally capture a ? followed by anything except whitespace (the querystring)
)? - close group, make it optional
)? - close group, make it optional
) - close group
@
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.