java.util.regex.PatternSyntaxException

时间:2014-09-15 08:47:15

标签: java android regex bluetooth

我修改了BluetoothChat代码,以便从微控制器接收到我的Android手机的串行数据。当我运行以下代码片段时,它会提供java.util.regex.PatternSyntaxException。究竟是什么错误?为什么会发生这种异常?

BluetoothChat.java

// The Handler that gets information back from the BluetoothChatService
private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {

        case MESSAGE_WRITE:

            byte[] writeBuf = (byte[]) msg.obj;
            // construct a string from the buffer
            String writeMessage = new String(writeBuf);
            mConversationArrayAdapter.add(writeMessage);
            break;

        case MESSAGE_READ:

            byte[] readBuf = (byte[]) msg.obj;       

           // construct a string from the valid bytes in the buffer

            String readMessage = new String(readBuf, 0, msg.arg1);

            if(readMessage.matches("*"))
            {
               mConversationArrayAdapter.add("Left");
            }
            else

            {mConversationArrayAdapter.add(readMessage);}

 }

登入猫:

      09-15 13:54:32.070: E/AndroidRuntime(21931): FATAL EXCEPTION: main
      09-15 13:54:32.070: E/AndroidRuntime(21931): java.util.regex.PatternSyntaxException: Syntax         error in regexp
 pattern near index 1:
      09-15 13:54:32.070: E/AndroidRuntime(21931): *
      09-15 13:54:32.070: E/AndroidRuntime(21931):  ^
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at java.util.regex.Pattern.compileImpl(Native Method)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at java.util.regex.Pattern.compile(Pattern.java:400)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at java.util.regex.Pattern.<init>(Pattern.java:383)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at java.util.regex.Pattern.matches(Pattern.java:413)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at java.lang.String.matches(String.java:1765) 09-15 13:54:32.070:
 E/AndroidRuntime(21931):   at
 com.example.footforthought.Sixth$2.handleMessage(Sixth.java:289)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at android.os.Handler.dispatchMessage(Handler.java:99)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at android.os.Looper.loop(Looper.java:137)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at   android.app.ActivityThread.main(ActivityThread.java:4517)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at java.lang.reflect.Method.invokeNative(Native Method)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at java.lang.reflect.Method.invoke(Method.java:511)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at  .android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

matches()使用正则表达式(正则表达式)来匹配输入,*是正则表达式元字符,意味着前一个标记为零或更多。由于之前没有令牌,因此语法错误。

如果你想按字面意思匹配*,请用反斜杠转义它:

readMessage.matches("\\*")

或使用一些字符串匹配函数,例如equals()不使用正则表达式。

答案 1 :(得分:1)

这是你的问题:

if(readMessage.matches("*")

如果您想匹配所有内容,请使用".*

此外,像 laalto 在他的回答中说。如果您想匹配所有内容,则必须使用*或仅使用两个斜杠Pattern.quote()来转义"\\*"