Examples of java match regular expression matching functions
Match telephone regular expression
Public class main {
Public static void main (string args []) {
String phone = "(111)-111-1111 ";
String phonenumberpattern = "(d -)? (D {3 }-)? D {3}-d {4 }";
System. out. println (phone. matches (phonenumberpattern ));
}
}
Zip code regular
Public class main {
Public static void main (string [] ){
String zip = "1234-123 ";
String zipcodepattern = "d {5} (-d {4 })? ";
Boolean retval = zip. matches (zipcodepattern );
}
}
Date Regular expression
Public class main {
Public static void main (string [] argv) throws exception {
Boolean retval = false;
String date = "12/12/1212 ";
String datepattern = "d {1, 2}-d {1, 2}-d {4 }";
Retval = date. matches (datepattern );
}
}
Username regular expression
Public class main {
Public static void main (string args []) {
Boolean retval = false;
String name = "first last ";
String nametoken = "p {upper} (p {lower} + s ?) ";
String namepattern = "(" + nametoken + ") {2, 3 }";
Retval = name. matches (namepattern );
}
}