使用regex android在过滤器名称中获取错误

时间:2015-06-19 08:08:46

标签: android regex

我会允许用户输入名称,允许他们使用此字符" a到z,A到Z,0到9,逗号,空格,下划线,短划线"只要。这是我的代码......

Pattern pattern_checkName = Pattern.compile("^[a-zA-Z0-9_-,//s//.]{3,}$");
Matcher matcher = pattern_checkName.matcher(para);
return matcher.matches();

我得到了这个错误,正则表达式语法错误:

06-19 14:27:29.653: E/AndroidRuntime(7735): java.util.regex.PatternSyntaxException: In a character range [x-y], x is greater than y near index 
14:06-19 14:27:29.653: E/AndroidRuntime(7735): ^[a-zA-Z0-9_-,//s//.]{3,}$
06-19 14:27:29.653: E/AndroidRuntime(7735):   
        ^
06-19 14:27:29.653: E/AndroidRuntime(7735):     at java.util.regex.Pattern.compileImpl(Native Method)

06-19 14:27:29.653: E/AndroidRuntime(7735):     at java.util.regex.Pattern.compile(Pattern.java:411)

06-19 14:27:29.653: E/AndroidRuntime(7735):     at java.util.regex.Pattern.<init>(Pattern.java:394)

06-19 14:27:29.653: E/AndroidRuntime(7735):     at java.util.regex.Pattern.compile(Pattern.java:381)

06-19 14:27:29.653: E/AndroidRuntime(7735):     at com.elite.mm.util.Util.checkName(Util.java:103)

06-19 14:27:29.653: E/AndroidRuntime(7735):     at com.elite.mm.ActivityRegister$1.onClick(ActivityRegister.java:89)

06-19 14:27:29.653: E/AndroidRuntime(7735):     at android.view.View.performClick(View.java:4438)

 06-19 14:27:29.653: E/AndroidRuntime(7735):    at android.view.View$PerformClick.run(View.java:18422)

 06-19 14:27:29.653: E/AndroidRuntime(7735):    at android.os.Handler.handleCallback(Handler.java:733)

 06-19 14:27:29.653: E/AndroidRuntime(7735):    at android.os.Handler.dispatchMessage(Handler.java:95)

 06-19 14:27:29.653: E/AndroidRuntime(7735):    at android.os.Looper.loop(Looper.java:136)

 06-19 14:27:29.653: E/AndroidRuntime(7735):    at android.app.ActivityThread.main(ActivityThread.java:5001)

 06-19 14:27:29.653: E/AndroidRuntime(7735):    at java.lang.reflect.Method.invokeNative(Native Method)

 06-19 14:27:29.653: E/AndroidRuntime(7735):    at java.lang.reflect.Method.invoke(Method.java:515)

 06-19 14:27:29.653: E/AndroidRuntime(7735):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
 06-19 14:27:29.653: E/AndroidRuntime(7735):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

 06-19 14:27:29.653: E/AndroidRuntime(7735):    at dalvik.system.NativeStart.main(Native Method)

非常感谢任何建议。 感谢

1 个答案:

答案 0 :(得分:0)

首先,当您在角色类中使用-时,正则表达式引擎将其视为文本范围,如果您想将其与文字相匹配,则需要将其放在最后人物类。

此外,如果您想匹配空格,则需要\s,以便使用以下正则表达式:

"^[a-zA-Z0-9_,\s//.-]{3,}$"

更精确而不是a-zA-Z0-9_你可以起诉\w匹配任何单词字符,并使用空格而不是空格也如果你不想使用点你应该从你的字符类:

"^[\w, /-]{3,}$"