有效网站链接的正则表达式

时间:2010-09-16 12:48:57

标签: regex

在我的asp.net应用程序中,我需要验证有效网站链接的文本。我想使用正则表达式验证器。任何知道如何验证weblink用户正则表达式的人。

2 个答案:

答案 0 :(得分:10)

试试这个 -

^(?:ftp|http|https):\/\/(?:[\w\.\-\+]+:{0,1}[\w\.\-\+]*@)?(?:[a-z0-9\-\.]+)(?::[0-9]+)?(?:\/|\/(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+)|\?(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+))?$

解释下面的每一步 -

^                                   # Start at the beginning of the text  
(?:ftp|http|https):\/\/              # Look for ftp, http, or https  
(?:                                  # Username:password combinations (optional)    
  [\w\.\-\+]+                        # A username    
  :{0,1}                             # an optional colon to separate the username and password    
  [\w\.\-\+]*@                       # A password  
)?  
(?:[a-z0-9\-\.]+)                    # The domain limiting it to just allowed characters  
(?::[0-9]+)?                         # Server port number  
(?:                                  # The path (optional)    
  \/|                                # a forward slash    
  \/(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+)| # or a forward slash followed by a full path    
  \?(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+)  # or a question mark followed by key value pairs   
)?$

答案 1 :(得分:1)

|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i