串口协议正则解析

规则匹配

1
2
3
4
5
6
7
8
9
10
11
12
// BB9716A33000112233445566778899001122FD6F030000010055740D0A
public static final String regex = "bb((..)*?)0d0a"; // 正则
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Observable.create((ObservableEmitter<String> emitter) -> {
String hexString = "BB9716A33000112233445566778899001122FD6F030000010055740D0ABB9716A33000112233445566778899001122FD6F030000010055740D0A";
Matcher mMatcher = pattern.matcher(hexString);
while (mMatcher.find()) {
String hexString = mMatcher.group();
emitter.onNext(hexString);// BB9716A33000112233445566778899001122FD6F030000010055740D0A 匹配到的每一个发到下级
}
emitter.onComplete();
})

长度匹配

1
2
3
4
5
6
7
8
9
10
11
12
// aabb060107ac6660aa
public static final String regex = "aa((..){8})"; // 正则
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Observable.create((ObservableEmitter<String> emitter) -> {
String hexString = "aabb060107ac6660aaaabb060107ac6660aaaabb060107ac6660aa";
Matcher mMatcher = pattern.matcher(hexString);
while (mMatcher.find()) {
String hexString = mMatcher.group();
emitter.onNext(hexString);// aabb060107ac6660aa 匹配到的每一个发到下级
}
emitter.onComplete();
})