java.util.regex.PatternSyntaxException错误

时间:2014-07-04 11:44:29

标签: android regex eclipse arrays

我正在努力学习我的项目,当用户尝试填写editTextField时,我会收到这些错误消息。 我需要帮助才能解决这个问题。

07-04 09:30:55.431: E/AndroidRuntime(16567): java.util.regex.PatternSyntaxException: Missing closing bracket in character class near index 34:
07-04 09:30:55.431: E/AndroidRuntime(16567): ^[?[A-Z,a-z]{3}[0-9]{2}[-][0-9]{5}
07-04 09:30:55.431: E/AndroidRuntime(16567):                                   ^
07-04 09:30:55.431: E/AndroidRuntime(16567):    at java.util.regex.Pattern.compileImpl(Native Method)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at java.util.regex.Pattern.compile(Pattern.java:407)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at java.util.regex.Pattern.<init>(Pattern.java:390)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at java.util.regex.Pattern.matches(Pattern.java:420)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at org.sine.test.FormActivity.isValid(FormActivity.java:527)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at org.sine.test.FormActivity$1$2.afterTextChanged(FormActivity.java:235)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at android.widget.TextView.sendAfterTextChanged(TextView.java:7354)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9112)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:673)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:197)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at android.os.Looper.loop(Looper.java:213)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at android.app.ActivityThread.main(ActivityThread.java:5225)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at java.lang.reflect.Method.invokeNative(Native Method)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at java.lang.reflect.Method.invoke(Method.java:525)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-04 09:30:55.431: E/AndroidRuntime(16567):    at dalvik.system.NativeStart.main(Native Method)

代码:

LinearLayout Elementslist = (LinearLayout) findViewById(R.id.view);
                    // LIST
                    for (int i = 0; i < form.getElementsArray().size(); i++) {
                        LayoutInflater inflator = (FormActivity.this)
                                .getLayoutInflater();
                        View convertView = inflator.inflate(R.layout.form_row,
                                null);

                        final RElement ele = form.getElementsArray().get(i);

                        TextView desc = (TextView) convertView
                                .findViewById(R.id.desc);
                        final EditText edit = (EditText) convertView
                                .findViewById(R.id.edit);

                        desc.setText(ele.getDescription());

                        edit.setError(getResources().getString(
                                R.string.required));

                        final int pos = i;

                        edit.addTextChangedListener(new TextWatcher() {

                            public void afterTextChanged(Editable s) {
                                if (ele.getRegularExpression().equals("")) {
                                    if (isValid(edit,
                                            ele.getRegularExpression(),
                                            ele.getDescription(), false)) {
                                        ele.setValue(edit.getText().toString());
                                        form.getReadies().set(pos, true);
                                    } else
                                        form.getReadies().set(pos, false);
                                } else {
                                    if (isValid(edit,
                                            ele.getRegularExpression(),
                                            ele.getDescription(), true)) {
                                        ele.setValue(edit.getText().toString());
                                        form.getReadies().set(pos, true);
                                    } else
                                        form.getReadies().set(pos, false);
                                }
                            }

                            public void beforeTextChanged(CharSequence s,
                                    int start, int count, int after) {
                            }

                            public void onTextChanged(CharSequence s,
                                    int start, int before, int count) {
                            }

                        });

                        Elementslist.addView(convertView);
                    }

但它指向了这个:

   public static boolean isValid(EditText editText, String regex,
        String errMsg, boolean required) {
    String text = editText.getText().toString().trim();
    editText.setError(null);
    if (required && !(editText.getText().length() != 0)) {
        editText.setError(res.getString(R.string.empty));
        return false;
    }
    if (required && !Pattern.matches(regex, text)) {
        editText.setError(res.getString(R.string.invalid));
        return false;
    }
    return true;
}

1 个答案:

答案 0 :(得分:0)

我发现我的代码运行良好,服务器上使用的正则表达式是一个问题。