Java 如何在Android中使用自定义正则表达式验证EditText输入?

Java 如何在Android中使用自定义正则表达式验证EditText输入?,java,android,regex,validation,Java,Android,Regex,Validation,我不熟悉android开发,也不熟悉正则表达式。我能够通过EditText从用户检索输入,并检查它是否为空,如果为空,稍后会显示错误消息,但我不确定如何使用自定义正则表达式进行检查。这是我的密码: myInput = (EditText) findViewById(R.id.myInput); String myInput_Input_a = String.valueOf(myInput.getText()); //replace if input contains whiteSpace S

我不熟悉android开发,也不熟悉正则表达式。我能够通过EditText从用户检索输入,并检查它是否为空,如果为空,稍后会显示错误消息,但我不确定如何使用自定义正则表达式进行检查。这是我的密码:

 myInput = (EditText) findViewById(R.id.myInput);
String myInput_Input_a = String.valueOf(myInput.getText());

//replace if input contains whiteSpace
String myInput_Input = myInput_Input_a.replace(" ","");


    if (myInput_Input.length()==0 || myInput_Input== null ){

               myInput.setError("Something is Missing! ");
     }else{//Input into databsae}

因此,我希望用户输入一个5个字符长的字符串,其中前2个字母必须是数字,最后3个字符必须是字符。那么,我如何在员工中实施it呢

根据正则表达式检查输入的常规模式:

String regexp = "\\d{2}\\D{3}"; //your regexp here

if (myInput_Input_a.matches(regexp)) {
    //It's valid
}
上面的实际正则表达式假设您实际指的是2个数字/数字(相同的东西)和3个非数字。相应地调整

regexp的变体:

"\\d{2}[a-zA-Z]{3}"; //makes sure the last three are constrained to a-z (allowing both upper and lower case)
"\\d{2}[a-z]{3}"; //makes sure the last three are constrained to a-z (allowing only lower case)
"\\d{2}[a-zåäöA-ZÅÄÖ]{3}"; //makes sure the last three are constrained to a-z and some other non US-ASCII characters (allowing both upper and lower case)
"\\d{2}\\p{IsAlphabetic}{3}" //last three can be any (unicode) alphabetic character not just in US-ASCII

数字和数字是一样的,请解释您想要匹配哪种类型的字符串?旁注:在调用某个变量的方法之前,您应该检查该变量是否为null。@cricket\u 007复制该变量,可以解决该问题。谢谢:)哇。谢谢你回复我一个解决方案。我现在正在做。会让你知道事情的进展:谢谢你的帮助。根据您的示例大致了解它的工作原理,是的,它正在工作!:D