Regular expression matching URL

Source: Internet
Author: User

Recently doing some of the URL matching project, originally on the regular expression of not much understanding, Internet search a lot of predecessors wrote regular expressions, found that very few can be used by me, now I write the regular expression to paste out, for your reference.

%< I do is an ASP. NET item that blocks some url>% in a text box

First, the regular expression:

String check = @ "((HTTP|FTP|HTTPS)://) (([a-za-z0-9\._-]+\.[ a-za-z]{2,6}) | ([0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3})) (: [0-9]{1,4}) * (/[a-za-z0-9\&%_\./-~-]*)? ";

A description of the regular expression:

①: The regular expression matches a string that must begin with the/HTTP, https://, ftp://;

②: The regular expression can match the URL or IP address; (e.g., http://www.baidu.com or http://192.168.1.1)

③: The regular expression can match to the end of the URL, which can match to the child URL; (If matched: http://www.baidu.com/s?wd=a&rsv_spt=1&issp=1&rsv_bp=0&ie= utf-8&tn=baiduhome_pg&inputt=1236)

④: The regular expression can match the port number;

Block some of the specified URLs:

If we want to block HTTP.//In the Input text box www.baidu.com This URL, the traditional method is to use the above regular expression to match the URL in the text box, read out all the URLs after the URL to be blocked to compare, but this method has a disadvantage, that is, we read the URL is a child URL, and we may write a parent URL in the configuration file, which Sample to the check out the URL to cut, plus the site default port number is: 80, we want to compare the port number and so on, I came up with a new method:

Reads the URL to block from the configuration file, forming a regular expression that matches the text box and blocks if it matches.

The configuration file should read: <add key= "Domaincheckblackurl" value= "baidu.com"/>

Implemented in code:

Now a regular expression consists of 3 parts:

1: The beginning of the regular expression, may consist of any character;

2: The middle part of the regular expression: the part read from the configuration file;

3: The end of the regular expression: there may be some sub-directories or port numbers, etc.;

First, read url:string[] serverlist = configurationmanager.appsettings["Domaincheckblackurl" from the configuration file). Split (', '); (in the configuration file, split with ",")

Second, string start = @ "((HTTP|FTP|HTTPS)://) ([a-za-z0-9_-]+\.) *"; (the beginning of the regular expression)

Then, the end of the regular expression: end = @ "(: [0-9]{1,4})? ((/[a-za-z0-9\&%_\./-~-]*) | (? =[^a-za-z0-9\.])) ";

Regular expression after composition: string check = start + @ "((? <=[^a-za-z0-9]) (" + Cutstr + ")" + END;

These are some of my small ideas, I hope to be helpful to everyone.

Regular expression matching URL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.