Data Validation with the Pattern and Matcher Objects

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Data Validation with the Pattern and Matcher Objects

This Java example program shows how to Validate date using the Pattern and Mater Objects.

[edit] Java Source Code

  • Package: example.validation
  • File: ValidationTestWithPatternAndMatcher.java
package example.validation;
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
 
public class ValidationTestWithPatternAndMatcher {
    public static void main(String args[]) {
	Pattern p = null;
	try {
	    p = Pattern.compile("Java \\d");
	} catch (PatternSyntaxException pex) {
	    pex.printStackTrace();
	    System.exit(0);
	}
 
	String candidate = "I love Java 5";
	Matcher m = p.matcher(candidate);
 
	System.out.println("result=" + m.find());
    }
}

[edit] What Result You Can Get

Run the program, you will get:

result=true

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools