Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the regular expression of matching URL URL in asp.net

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to achieve the regular expression of matching URL URL in asp.net, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

%%

First, regular expressions:

String check = @ "((http | ftp | https): / /) ([a-zA-Z0-9\. _ -] +. [a-zA-Z] {2 a-zA-Z 6}) | ([0-9] {1 a-zA-Z0 3}\. [0-9] {1 J 3}\. [0-9] {1 Jue 3}\. [0-9] {1 Jue 3})) (: [0-9] {1 no 4}) * (/ [a-zA-Z0-9\ &% _\. /-~] *)?"

A description of the regular expression:

①: the string matched by this regular expression must start with http://, https://, ftp://

②: this 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 URL, that is, to the child URL; (if matching: http://www.baidu.com/s?wd=a&rsv_spt=1&issp=1&rsv_bp=0&ie=utf-8&tn=baiduhome_pg&inputT=1236)

④: this regular expression can match the port number

Block some specified URL:

If we want to block the URL of http://www.baidu.com in the input text box, the traditional method is to use the above regular expression to match the URL in the text box, and after reading out all the URL, compare it with the URL to be blocked, but this method has a disadvantage, that is, we read the URL all the way to the child URL, while we may write a parent URL in the configuration file, so we have to cut the URL out of the check. Plus the default port number of the website is: 80, we are going to compare the port number and so on, I have come up with a new method:

Read out the URL to be blocked from the configuration file to form a regular expression to match the text box, and block it if it can be matched.

It should be written in the configuration file:

Implement in the code:

Now a regular expression consists of three parts:

1: the beginning of a regular expression, which 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 subdirectories or port numbers, etc.

First, read URL:string [] serverlist = ConfigurationManager.AppSettings ["DomainCheckBlackUrl"] .split (',') from the configuration file (split by "," in the configuration file)

Secondly, string start = @ "((http | ftp | https): / /) ([a-zA-Z0-9 expressions -] +\.) *"; (the beginning of the regular expression)

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

The regular expression after the combination: string check = start + @ "(?

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report