java - 在 Java 中验证特定的字符串模式
问题描述
我想将输入字符串的模式验证为两个数字,然后是三个大写字母,然后是三个数字。
例如:“16FIT146”字符串应该是有效的。
解决方案
你可以这个正则表达式
String pattern = "^[0-9]{2}[A-Z]{3}[0-9]{3}$";
if (str.matches(pattern)) {
// something here.
}
例子
public static void main(String []args){
matches("16FIT146");
matches("anything");
}
public static void matches(String str) {
String pattern = "^[0-9]{2}[A-Z]{3}[0-9]{3}$";
if (str.matches(pattern)) {
System.out.println("matches");
} else {
System.out.println("not matches");
}
}
推荐阅读
- java - 用文本替换内容控件
- mysql - 如何在laravel中检测内部服务器错误(500)
- ruby-on-rails - 在同一线程中使用辅助/自治事务
- java - 在哪里为 java 中的 SSLException 添加证书?
- python-3.x - Pip 无法正常工作,我似乎无法找到解决方法
- square - SquarePOS - 为什么仪表板上没有出现销售?
- validation - ckeditor | 内的“必需”属性
- c# - 是否可以在 C# wpf(网格)中绘制形状,如果网格按行和单元格拆分,并且每行内部包含一个堆栈面板?
- excel - Excel 数据透视表 VBA:如何将总和放入行标签中
- laravel - 在 laravel 中,数据未插入数据库