Java Regular Expressions add tags to url links

Source: Internet
Author: User

Java code
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
 
Public class HtmlTag {
Public static void main (String [] args ){
String str = "this is a url link http://www-test.company.com/view/1_2.html? A = % B8 & f = % E4 + % D3 # To convert td to clickable ";
String regex = "(http: | https :) // [^ [A-Za-z0-9 \\._\\? % & + \-=/#] * ";
Pattern pattern = Pattern. compile (regex );
Matcher matcher = pattern. matcher (str );
StringBuffer result = new StringBuffer ();
While (matcher. find ()){
StringBuffer replace = new StringBuffer ();
Replace. append ("<a href = \" "). append (matcher. group ());
Replace. append ("\" target = \ "_ blank \"> click to open the link </a> ");
Matcher. appendReplacement (result, replace. toString ());
}
Matcher. appendTail (result );
System. out. println (str );
System. out. println (result );
}
}

Author "wsy495"
 

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.