Mega Code Archive

 
Categories / Java Tutorial / Regular Expressions
 

Use Pattern class to match

import java.util.regex.Matcher; import java.util.regex.Pattern; public class MainClass {   public static void main(String args[]) {     Pattern p = Pattern.compile("B(on)d");     String candidateString = "My name is Bond. James Bond.";     String matchHelper[] = { "               ^", "              ^", "                           ^",         "                          ^" };     Matcher matcher = p.matcher(candidateString);     // find the end point of the second sub group (ond)     int nextIndex = matcher.end(1);     System.out.println(candidateString);     System.out.println(matchHelper[3] + nextIndex);   } }