java match正则匹配函数几个实例
match电话正则
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));
}
}
邮编号正则
public class main {
public static void main(string[] a) {
string zip = "1234-123";
string zipcodepattern = "d{5}(-d{4})?";
boolean retval = zip.matches(zipcodepattern);}
}
日期正则
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);}
}
用户名正则表达式
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);
}
}