2016-12-08 30 views
0

我正在使用正則表達式來驗證c#中的URL。 正則表達式是:正則表達式:使用端口號驗證URL

^((((http(s)?|ftp(s)?):(?:\/\/)?)(?:[-;:&=\+\$,\w][email protected])?[A-Za-z0-9.-]+|(?:([a-zA-Z]{1,}\.)|[-;:&=\+\$,\w][email protected])[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$

它是爲像URL做工精細:http://stackoverflow.com,stackoverflow.com但我也想驗證http://localhost:1234和本地主機:1234。

+1

參見[?什麼是最好的正則表達式來檢查一個字符串是否是一個有效的URL](http://stackoverflow.com/questions/161738/what- is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) –

+1

它必須是可能的。一切都可能與正則表達式。即使解析HTML是[幾乎可能](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454)。如果您想在2017年2月瞭解自己的代碼,請使用其他方法。 –

回答

0

試試這個表達式來代替:

^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$ 
+0

它也驗證「http:// localhost」。 –