Saturday, July 18, 2009

Regular Expression Using String Class

You can use Java Regular Expression for performaing complex validation on String objects.
You can also use matchs() method of String class for performaing Regular Expression. This function returns boolean value, return "true" if string object passes the regular expression validation other wise return "false".
Example:
String namePattern = "[a-zA-Z_]+";
boolean retval = name.matches(namePattern);
if(retval)
System.out.println("Contains only alphabet");
else
System.out.println("validation fail");
pattern can take only alphabet of any case and underscore of any length.

No comments:

Post a Comment