使用下面的語法爲您的Java模式,爲了找到Android + version
令牌:
String[] test = {
"Dalvik/1.6.0 (Linux; U; Android 4.4.4; R8207 Build/KTU84P)",
"Dalvik/2.1.0 (Linux; U; Android 5.1.1; A51 Build/LMY47V)",
"Dalvik/2.1.0 (Linux; U; Android 5.0; vivo X5Pro D Build/LRX21M)",
"Dalvik/1.6.0 (Linux; U; Android 4.4.4; R8207 Build/KTU84P)",
"Dalvik/1.6.0 (Linux; U; Android 4.4.2; TCL P331M Build/KOT49H)"
};
Pattern p = Pattern.compile("Android\\s[\\d.]+");
Pattern wholeStringMatch = Pattern.compile("^.*?(Android\\s[\\d.]+).*?$");
System.out.printf("Matching only necessary pattern...%n%n");
for (String s: test) {
Matcher m = p.matcher(s);
if (m.find()) {
System.out.printf("Found: %s%n", m.group());
}
}
System.out.printf("%nWhole match...%n%n");
for (String s: test) {
Matcher m = wholeStringMatch.matcher(s);
if (m.find()) {
System.out.printf("Found: %s in %s%n", m.group(1), m.group());
}
}
輸出
Matching only necessary pattern...
Found: Android 4.4.4
Found: Android 5.1.1
Found: Android 5.0
Found: Android 4.4.4
Found: Android 4.4.2
Whole match...
Found: Android 4.4.4 in Dalvik/1.6.0 (Linux; U; Android 4.4.4; R8207 Build/KTU84P)
Found: Android 5.1.1 in Dalvik/2.1.0 (Linux; U; Android 5.1.1; A51 Build/LMY47V)
Found: Android 5.0 in Dalvik/2.1.0 (Linux; U; Android 5.0; vivo X5Pro D Build/LRX21M)
Found: Android 4.4.4 in Dalvik/1.6.0 (Linux; U; Android 4.4.4; R8207 Build/KTU84P)
Found: Android 4.4.2 in Dalvik/1.6.0 (Linux; U; Android 4.4.2; TCL P331M Build/KOT49H)
注意
- 您可以更換
\\w+
元素只是靜態的Android
因爲你知道這是不變的。
- 添加了一個完整的字符串匹配模式來測試整個字符串。正如你所看到的,你正在尋找的令牌現在回參考組
1
你在'\ w +'之後沒有轉義過'/'。糾正它並再次檢查。 –
只需加倍Java中的反斜槓即可。像'(\\ w + /(?:\\ d \\。){1,2} \\ d)(\\(Linux; U;)(Android(?:\\ d \\。){ 2} \\ d)(。*)'',並且逃避那些與字面點匹配的點 –
謝謝@ noob,@ WiktorStribiżew,但是這在我的hql中不起作用'select regexp_extract(ua,'(\\ w + /(?:\\ d \\。){1,2} \\ d)(\\(Linux; U;)(Android(?:\\ d \\。){1,2} \\ d) (。*)',3) from vlock_cleaned.vlock_base_active;' – Ivo