使用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 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短