使用Java正则表达式验证邮政编码
为了使用正则表达式匹配邮政编码,我们在Java中使用matchs方法。java.lang.String.matches()方法返回一个布尔值,该值取决于String与正则表达式的匹配。
声明-java.lang.String.matches()方法的声明如下-
public boolean matches(String regex)
让我们看一个使用Java正则表达式验证邮政编码的程序-
示例
public class Example {
public static void main( String[] args ) {
System.out.println(zipIndia("400709"));
System.out.println(zipUS("10060"));
}
//验证zip-
public static boolean zipIndia( String z ) {
return z.matches( "\\d{6}" );
}
public static boolean zipUS( String z ) {
return z.matches( "\\d{5}" );
}
}输出结果
true true
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短