我正在编写一个 Android 应用程序,其中需要对通过蓝牙接收的数据进行正则表达式检查。
对于每个接收到的行,数据表达式应该为A= 137, B= 137, C= 138, D= 136, E= 134, F= 138, G= 137
。我想首先确保数据匹配“A= ddd,B= ddd.....G= ddd”,而d,值可以是0-1023。然后,提取A到G的值并将它们放入A到G的数组中。我尝试过匹配它,但总是失败。我知道Android studio中有一个检查正则表达式的功能,我也使用在线网站进行检查。它们都匹配,但在应用程序中失败。
有人可以帮忙吗?下面是从蓝牙套接字接收和匹配的数据。
void beginListenForData()
{
Log.d(TAG,"begin for listening");
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
Log.d(TAG,"runnable thread");
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
// Log.d(TAG,"incoming data is available");
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
Log.d(TAG, "data = " + data);
handler.post(new Runnable()
{
public void run()
{
textView.setText(data);
}
});
if (data.matches("A= \\d*, B= \\d*, C= \\d*, D= \\d*, E= \\d*, F= \\d*, G= \\d*")){
Log.d(TAG, "data matches");
Pattern dataA = Pattern.compile("(?<== )\\d*");
Matcher matcherA = dataA.matcher(data);
Log.d(TAG, matcherA.group());
}else{
handler.post(new Runnable()
{
public void run()
{
textView.setText("invalid data");
}
});
}
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
编辑:
用于打印 Log.d(TAG, "data = "+ data);
中的数据:
2020-02-14 00:43:21.768 5130-5297/com.example.insoleapp D/Signal Fragment: data = A= 137, B= 138, C= 142, D= 138, E= 139, F= 138, G= 138
2020-02-14 00:43:21.841 5130-5297/com.example.insoleapp D/Signal Fragment: data = A= 137, B= 138, C= 141, D= 137, E= 136, F= 134, G= 138
2020-02-14 00:43:21.971 5130-5297/com.example.insoleapp D/Signal Fragment: data = A= 138, B= 139, C= 140, D= 139, E= 138, F= 138, G= 136
2020-02-14 00:43:22.080 5130-5297/com.example.insoleapp D/Signal Fragment: data = A= 138, B= 140, C= 138, D= 137, E= 133, F= 139, G= 139
2020-02-14 00:43:22.183 5130-5297/com.example.insoleapp D/Signal Fragment: data = A= 138, B= 137, C= 139, D= 139, E= 138, F= 138, G= 138
2020-02-14 00:43:22.287 5130-5297/com.example.insoleapp D/Signal Fragment: data = A= 139, B= 138, C= 139, D= 137, E= 138, F= 139, G= 141
2020-02-14 00:43:22.398 5130-5297/com.example.insoleapp D/Signal Fragment: data = A= 140, B= 137, C= 141, D= 138, E= 137, F= 137, G= 140
最佳答案
您正在从二进制数据中检索字符串。这意味着它们可能包含不是可打印字符而是控制字符的字符。这些与您的表达式不匹配,因此表达式填充失败。
需要关注的主要字符是 00
字符串结尾字符,该字符常用于 C 环境和可能的汇编中。这些通常会在 JNI 级别被错误地复制到 Java 字符串中,或者被不幸的开发人员自己复制到 Java 字符串中。 Java 字符串对象具有明确的大小,并且不依赖于丑陋的空终止符。
要删除末尾的控制字符,您可以调用String#trim()
,以便删除控制字符。来自 JavaDoc:
Returns a string whose value is this string, with all leading and trailing space removed, where space is defined as any character whose codepoint is less than or equal to 'U+0020' (the space character).
诸如此类的控制字符位于0x0020
下方。
但是,您当然最好首先创建没有此类控制字符或字符串结尾的String
。
关于java - Java中的正则表达式无法匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60212687/