URL Java Regular expressions in matching text __ regular expressions

Source: Internet
Author: User
Tags net domain

Division has a business need to match the URL in the text, the degree Niang, gu elder brother a half-day often see have to get hyperlinks in HTML, as long as the matching <a href=[' "]? (.*?) [']? Yes, but if it's an ordinary text type, it's a bit more cumbersome to share the final expression:

Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern; public class Urlmatcher {public static void main (string[] args) {Pattern pattern = pattern. Compile ("Http://|ftp ://|https://|www) {0,1}[^\u4e00-\u9fa5\\s]*?\\. (COM|NET|CN|ME|TW|FR)
		[^\u4e00-\u9fa5\\s]* ");
		Space end Matcher Matcher = pattern. Matcher ("Download address http://www.zuidaima.com/sdfsdf.htm?aaaa=%ee%sss? sdfsyyy space End");
		while (Matcher.find ()) {System.out.println (Matcher.group (0));
		///Chinese end matcher = pattern. Matcher ("Download address http://www.zuidaima.com/sdfsdf.htm?aaaa=%ee%sss URL end");
		while (Matcher.find ()) {System.out.println (Matcher.group (0));
		///no http://opening matcher = pattern. Matcher ("Download address www.zuidaima.com/sdfsdf.htm?aaaa=%ee%sss URL end");
		while (Matcher.find ()) {System.out.println (Matcher.group (0));
		//NET domain name Matcher = pattern. Matcher ("Download address www.xxx.net/sdfsdf.htm?aaaa=%ee%sss URL end");
	while (Matcher.find ()) {System.out.println (Matcher.group (0));	//XXX domain name Matcher = pattern. Matcher ("Download address www.zuidaima.xxx/sdfsdf.htm?aaaa=%ee%sss URL end");
		while (Matcher.find ()) {System.out.println (Matcher.group (0));
		The//YYYY domain name does not match the System.out.println ("not match yyyy domain name");
		Matcher = pattern. Matcher ("Download address www.zuidaima.yyyy/sdfsdf.html?aaaa=%ee%sss URL end");
		while (Matcher.find ()) {System.out.println (Matcher.group (0));
		//No http://www.
		Matcher = pattern. Matcher ("Download address zuidaima.com/sdfsdf.html?aaaa=%ee%sss URL end");
		while (Matcher.find ()) {System.out.println (Matcher.group (0)); }

	}
}


The result is dead, but the idea is alive, everyone can rewrite the expression according to their own business.

There is a picture of the truth:

Related code Download: http://www.zuidaima.com/share/1575653789993984.htm reprint please indicate the source.

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.