我是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个字符必须是字符。那么我该如何实施呢?
用于根据正则表达式检查输入的常规模式:
String regexp = "\\d{2}\\D{3}"; //your regexp here
if (myInput_Input_a.matches(regexp)) {
//It's valid
}
上面的实际正则表达式假设您实际上是指2个数字/数字(相同的内容)和3个非数字。相应地调整。
正则表达式的变化:
"\\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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句