Pattern Matching

From Suhrid.net Wiki
Revision as of 08:56, 4 July 2011 by Suhridk (talk | contribs)
Jump to navigationJump to search
  • Classes in the java.util.regex package provide regular expressions support.
  • Basic example
import java.util.regex.*;

public class RegexTest1 {

	public static void main(String[] args) {
		
		Pattern p = Pattern.compile("lazy"); //The pattern to search for
		Matcher m = p.matcher("The quick brown fox jumps over the lazy dog"); //The source against which to match the pattern
		boolean found = false;
		while(m.find()) {
			System.out.println("Match found at " + m.start() + "," + m.end());
			found = true;
		}
		
		if(!found) {
			System.out.println("No match found");
		}
	}

}