• Java技能树-RE-正则应用-字符串篇


    示例索引(场景1:Java的String类)

    • 示例:获取应用的包名(去除前面N个字符,截取后面部分)
    • 示例:获取应用包名(去除后面N个字符,截取前面部分)
    • 示例:获取应用MD5(去除字符串中空格)
    • 示例:获取AppOps的log中的timestamp(字符串分割)
    • 示例:获取Windows路径中某个目录名字(字符串分割:\)
    • 示例:lsof获取联网应用的PID(字符串分割:无规律空格)
    • 示例:AppOps的log,判断是包名还是时间戳(判断字符串是否包含数字)


    示例:获取应用的包名(去除前面N个字符,截取后面部分)

    1. public class Test {
    2. public static void main(String[] args) {
    3. String s = "package:com.android.cts.priv.ctsshim";
    4. System.out.println(s.substring(8));
    5. }
    6. }


    示例:获取应用包名(去除后面N个字符,截取前面部分)

    1. public class Test {
    2. public static void main(String[] args) {
    3. String s = "com.android.cts.priv.ctsshim.apk";
    4. System.out.println(s.substring(0, s.length() - 4));
    5. }
    6. }


    示例:获取应用MD5(去除字符串中空格)

    1. public class Test {
    2. /**
    3. * 示例X:获取应用MD5
    4. * <p>
    5. * 字符串去除空格场景:
    6. * (1)去除首尾空格,使用trim方法
    7. * (2)去除第一个空格,使用replaceFirst方法
    8. * (3)去除所有空格,使用replace方法
    9. * (4)replaceAll:Pattern.compile(regex).matcher(str).replaceAll(repl)
    10. */
    11. public static void main(String[] args) {
    12. String md5 = " 0e 3b bd 10 30 a9 45 bb 42 f1 b5 71 48 b3 bc 07 ";
    13. System.out.println(md5.replace(" ", ""));
    14. }
    15. }


    示例:获取AppOps的log中的timestamp(字符串分割)

    1. public class Test {
    2. public static void main(String[] args) {
    3. String s = "Access: [top-s] 2022-05-28 11:10:02.657 (-53m8s469ms) duration=+709ms";
    4. System.out.println(s.split("\\(")[0].split("]")[1]);
    5. }
    6. }


    示例:获取Windows路径中某个目录名字(字符串分割:\)

    1. public class Test {
    2. public static void main(String[] args) {
    3. String s = "D:\\project\\PhoneSecScanner\\tmp\\class\\android.ext.services\\com\\google\\common\\collect\\MapMakerInternalMap.class";
    4. System.out.println(s.split("\\\\")[5]);
    5. }
    6. }

    参考资料:

    java基础:字符串分隔反斜杠\_椰果子的博客-CSDN博客_java split 反斜杠


    示例:lsof获取联网应用的PID(字符串分割:无规律空格)

    adb shell lsof | findstr IPv

    1. C:\>adb shell lsof | findstr IPv
    2. ims_rtp_daemon 3504 radio 19u IPv6 0t0 205132 UDP []:38088->[]:0
    3. ims_rtp_daemon 3504 radio 20u IPv6 0t0 205136 UDP []:45658->[]:0
    4. droid.bluetooth 4216 bluetooth 131u IPv4 0t0 665124 TCP :8872->:0 (LISTEN)

    思路1:lsof提取PID(字符串分割)

    • 为啥\s* 匹配结果一个一个字符
    1. public class Test {
    2. public static void main(String[] args) {
    3. String s = "ims_rtp_daemon 3504 radio 19u IPv6 0t0 205132 UDP []:38088->[]:0";
    4. String[] s1 = s.split("\\s+");
    5. String[] s2 = s.split("\\s*");
    6. for (String s3 : s1) {
    7. System.out.println(s3);
    8. }
    9. for (String s3 : s2) {
    10. System.out.println(s3);
    11. }
    12. }
    13. }

     

    参考资料:

    JAVA正则表达式匹配多个空格_予亭的博客-CSDN博客_正则匹配空格


    思路2:lsof 提取 PID(字符串替换)

    1. public class Test {
    2. public static void main(String[] args) {
    3. String s1 = "ims_rtp_daemon 3504 radio 19u IPv6 0t0 205132 UDP []:38088->[]:0";
    4. while (s1.contains(" ")) {
    5. s1 = s1.replace(" ", " ");
    6. }
    7. System.out.println(s1);
    8. }
    9. }

    示例:AppOps的log,判断是包名还是时间戳(判断字符串是否包含数字)

    1. import java.util.regex.Pattern;
    2. public class Test {
    3. public static void main(String[] args) {
    4. String s1 = "Package com.android.systemui:";
    5. String s2 = "Access: [pers-s] 2022-05-09 18:56:27.117 (-49d4h33m48s601ms)";
    6. System.out.println(Pattern.compile("\\d").matcher(s1).find());
    7. System.out.println(Pattern.compile("\\d").matcher(s2).find());
    8. }
    9. }


  • 相关阅读:
    HC-05蓝牙模块--------手机与STM32通信(代码编写)(上位机配置)保姆级教程
    ClickHouse快速安装-可视化工具连接-创建第一个ck库表(一)
    面试复盘四
    Fatal error: cstring: No such file or directory
    Day5 计算机网络分层结构——OSI、TCP/IP、五层参考模型
    代码与细节(一)
    nginx的反向代理和负载均衡
    rust -枚举和模式匹配学习
    OceanBase再获OSCAR两项大奖,坚定开源开放
    pyenv安装python,Makefile报错
  • 原文地址:https://blog.csdn.net/pwp032984/article/details/125494083