Author: techfox9

Java pattern matching example..

Friday, June 19th, 2009 @ 5:07 pm

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class TestPattern {
    public static void main(String[] args) {
	String pin = "1234567";
	String pinPattern = "[0-9]+";
	String result = null;

	Pattern pattern = Pattern.compile(pinPattern);
	Matcher matcher = pattern.matcher(pin);

	result = "";
	while(matcher.find()) {
	    result += matcher.group();
	}
	if (result.equals(pin)) {
	    result = "PIN accepted";
	}
	else {
	    result = "PIN invalid";
	}
	System.out.println(result);
    }
}

Java, JavaUsage


 


Comments are closed.