简体   繁体   中英

Data processing Using Java RegEx

There is sorts of data like

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)

I want get Android vcode like 'Android 4.4.2'. Regex pattern like (\\w+/(\\d.){1,2}\\d)( \\(Linux; U; )(Android (\\d.){1,2}\\d)(.*) works well. But when I wrote in Java then it doesnt work. How i write it in Java Regex Syntax. Any advice would be appreciated. Thank you!

Use the following syntax for your Java pattern, in order to find the Android + version token:

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());
    }
}

Output

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)

Notes

  • You can replace the \\\\w+ element with just static Android since you know this is constant.
  • Added a whole string match pattern to test against the whole string. As you can see, the token you're looking for is now back-referenced to group 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM