Regular Expression-matching url in a PHP string

Source: Internet
Author: User
Requirement: {code...} URL sample: {code...} Of course, simple URLs are also required to be matched to solve Regular Expressions:

When the client uploads a string, all URLs must be matched from the string, including parameters (including ports) after the domain name or IP address)

URL example:

Http: // 127.0.0.1/metinfo/img. php? Class1 = 1 & serch_ SQL = % 201 = if % 28 ascii % 28 substr % 28 user % 28% 29,1, 1% 29% 29 = 114,1, 2% 29% 23 or http://www.baidu.com/metinfo/img/img.php? Class1 = 1 & serch_ SQL = % 201 = if % 28 ascii % 28 substr % 28 user % 28% 1% 29%, 2% 29% 29 = 114,1, 23

Of course, simple URLs must also be matched.
Solving Regular Expressions

Reply content:

Requirements:

When the client uploads a string, all URLs must be matched from the string, including parameters (including ports) after the domain name or IP address)

URL example:

Http: // 127.0.0.1/metinfo/img. php? Class1 = 1 & serch_ SQL = % 201 = if % 28 ascii % 28 substr % 28 user % 28% 29,1, 1% 29% 29 = 114,1, 2% 29% 23 or http://www.baidu.com/metinfo/img/img.php? Class1 = 1 & serch_ SQL = % 201 = if % 28 ascii % 28 substr % 28 user % 28% 1% 29%, 2% 29% 29 = 114,1, 23

Of course, simple URLs must also be matched.
Solving Regular Expressions

Use a broad regular expression to match all URLs. For example

https?:\/\/\S+

Then useparse_urlFunction

^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$http://regexlib.com/Search.aspx?k=url&c=-1&m=5&ps=20

This is probably the case in Java.

String str = "received String" String regex = "(http: | https :) // [^ [A-Za-z0-9 ,:\\._\\? % & + \-=/#] * "; Pattern pattern = Pattern. compile (regex); Matcher matcher = pattern. matcher (str); while (matcher. find () {String url = matcher. group (); System. out. println (url );}

The following string is tested.

String str = "http: // 127.0.0.1: 6666/" + "https://www.baidu.com/" + "http: // 127.0.0.1/metinfo/img. php? Class1 = 1 & serch_ SQL = % 201 = if % 28 ascii % 28 substr % 28 user % 28% 1% 29%, 29 =, 2% 29% 23 \ n "+" or \ n "+" Haha http://www.baidu.com: 85676/metinfo/img. php? Class1 = 1 & serch_ SQL = % 201 = if % 28 ascii % 28 substr % 28 user % 28% 1% 29%, 2% 29% 29 = 114,1, 6666 23 are correct ";

Output

http://127.0.0.1:6666/https://www.baidu.com/http://127.0.0.1/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23http://www.baidu.com:85676/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23

What are you asking about PHP? Sorry, I don't know PHP...
The regular expression is the same. Move your head.

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.