Wednesday, December 31, 2008

Java: pattern matching

private void testPattern() {
String url = "/h/d/hi/280/en/hd/atlcp?tags=true";
// Pattern p = Pattern.compile( "(/hld/hi/en/hd/)[^ ]*" );
// Pattern p = Pattern.compile( "(/h/d/[a-z][a-z]/[a-z][a-z]/hd/)[^ ]*"
// );
Pattern p = Pattern
.compile("(/h/d/[a-z][a-z]/\\d+/[a-z][a-z]/hd/)([^ ])+");
Matcher m = p.matcher(url);
boolean found = m.matches();
System.out.println(found);

if (found) {
System.out.println("m.groupCount()=" + m.groupCount());
System.out.println(m.group(2));
System.out.println(m.group());
}
}

No comments:

Post a Comment