检查特定的字符串格式

时间:2014-03-20 19:33:57

标签: java string

如果输入的字符串符合aa/dddd所需的格式,a表示大写字母或d表示数字

,我将如何检查java?

3 个答案:

答案 0 :(得分:3)

使用简单的正则表达式:^ [A-Za-z] {2} / [0-9] {4} $

String regex = "^[A-Za-z]{2}/[0-9]{4}$";

String test = "ab/1232";
if (test.matches(regex)) {
    // my regex matches
}

答案 1 :(得分:1)

我相信

String.matches("[A-Za-z]{2}/\\d{4}")

String.matches("[A-Za-z]{2}/[0-9]{4}")

会起作用。

答案 2 :(得分:1)

试试这个,

String str="aF/1234";

if(str.matches("[a-zA-Z]{2}/\\d{4}"))
    System.out.println("Match");
相关问题