Saturday, May 16, 2009

Simple java Regular Expression matching

This simple regular expression matching code just checks for all hyper links in the input string and converts it into proper clickable links in html.

import java.util.regex.*;

public static void main(String args[]) {
System.out.println("test");

String test="sdsfssuwww.yah2oo.com/sds3d jisds sdsds www.google.com/sdsd.htm sds";
Pattern pattern = Pattern.compile("(http://)?www.[a-zA-Z0-9./]*");
Matcher matcher = pattern.matcher(test);

boolean found = false;

while (matcher.find()) {

String a = test.substring(0,matcher.start()-1);
String b =test.substring(matcher.end());
String c=a+"<a href=\""+matcher.group()+"\">"+matcher.group()+"</a> "+b;

System.out.println("a"+a);
System.out.println("b"+b);
System.out.println("c"+c);

found = true;

}
if (!found) {
System.out.println("No match found.");
}

}

No comments:

 
Free Domain Names @ .co.nr!