gpt4 book ai didi

Java Regex - 从 ps 命令中提取 PID

转载 作者:行者123 更新时间:2023-11-29 07:07:44 24 4
gpt4 key购买 nike

我有一个 android 设备的 ps 命令 的结果,我想提取特定进程的 PID。我尝试了多个正则表达式,但似乎无法使一个正常工作。
(我总是得到 java.lang.IllegalStateException: No match found)

这是我当前的 java 代码:

final String patternStr = "(?m)^\\w+?\\s+?(\\d+).+?(?:\\Q" + processName + "\\E)";
final Pattern pattern = Pattern.compile(patternStr);
final Matcher matcher = pattern.matcher(psResult);
final String processPID = matcher.group(1);

这是 psResult 变量的内容:

USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME
root 1 0 404 284 c0269b33 08056b46 S /init
root 2 0 0 0 c0226114 00000000 S kthreadd
root 3 2 0 0 c021ad72 00000000 S ksoftirqd/0
root 4 2 0 0 c02237d8 00000000 S events/0
root 5 2 0 0 c02237d8 00000000 S khelper
root 11 2 0 0 c02237d8 00000000 S suspend
root 161 2 0 0 c02237d8 00000000 S kblockd/0
root 165 2 0 0 c02237d8 00000000 S cqueue
root 172 2 0 0 c02237d8 00000000 S ata/0
root 173 2 0 0 c02237d8 00000000 S ata_aux
root 175 2 0 0 c02237d8 00000000 S ksuspend_usbd
root 180 2 0 0 c04209e5 00000000 S khubd
root 183 2 0 0 c0442d0a 00000000 S kseriod
root 188 2 0 0 c02237d8 00000000 S kmmcd
root 241 2 0 0 c0243c5e 00000000 S pdflush
root 242 2 0 0 c0243c5e 00000000 S pdflush
root 243 2 0 0 c0246b88 00000000 S kswapd0
root 298 2 0 0 c02237d8 00000000 S aio/0
root 479 2 0 0 c02237d8 00000000 S iscsi_eh
root 491 2 0 0 c040c750 00000000 S mtdblockd
root 492 2 0 0 c040c750 00000000 S nftld
root 524 2 0 0 c02237d8 00000000 S kstriped
root 527 2 0 0 c02237d8 00000000 S kondemand/0
root 532 2 0 0 c02237d8 00000000 S hid_compat
root 574 1 380 200 c0269b33 08056b46 S /sbin/ueventd
system 771 1 1260 388 c0480de8 b7f0d22a S /system/bin/servicemanager
root 773 1 5440 988 ffffffff b7ee2e51 S /system/bin/vold
root 775 1 11400 1572 ffffffff b7f68e51 S /system/bin/netd
root 776 1 1412 472 c048c1ee b7e4f2e3 S /system/bin/debuggerd
radio 777 1 6288 1032 ffffffff b8023e51 S /system/bin/rild
system 778 1 25344 11636 ffffffff b7f9522a S /system/bin/surfaceflinger
root 779 1 174012 36388 ffffffff b7f0c3ec S zygote
drm 780 1 9752 2888 ffffffff b7f0c22a S /system/bin/drmserver
media 781 1 40780 8224 ffffffff b7f3722a S /system/bin/mediaserver
install 782 1 1344 528 c04eed2d b8023e36 S /system/bin/installd
keystore 783 1 3140 1020 c048c1ee b7e972e3 S /system/bin/keystore
root 784 1 1312 468 c02814ee b7f48997 S /system/bin/qemud
shell 787 1 1340 660 c034e6bc b7f45e36 S /system/bin/sh
root 792 1 5596 292 ffffffff 080600f8 S /sbin/adbd
system 1181 779 260092 41728 ffffffff b7f0c22a S system_server
u0_a33 1248 779 190676 40204 ffffffff b7f0d997 S com.android.systemui
u0_a10 1284 779 188352 22336 ffffffff b7f0d997 S android.process.acore
u0_a18 1308 779 183328 21016 ffffffff b7f0d997 S com.android.inputmethod.latin
radio 1344 779 204860 26896 ffffffff b7f0d997 S com.android.phone
u0_a2 1356 779 196728 36612 ffffffff b7f0d997 S com.android.launcher
u0_a5 1374 779 180280 17164 ffffffff b7f0d997 S com.android.location.fused
system 1388 779 189852 19440 ffffffff b7f0d997 S com.android.settings
u0_a25 1455 779 182216 18864 ffffffff b7f0d997 S com.android.music
u0_a10 1468 779 189632 19820 ffffffff b7f0d997 S com.android.contacts
u0_a24 1482 779 185232 22180 ffffffff b7f0d997 S android.process.media
u0_a15 1542 779 185864 22168 ffffffff b7f0d997 S com.android.mms
u0_a22 1572 779 184668 21252 ffffffff b7f0d997 S com.android.deskclock
u0_a37 1590 779 188616 18732 ffffffff b7f0d997 S com.android.exchange
u0_a38 1605 779 197268 20328 ffffffff b7f0d997 S com.android.providers.calendar
u0_a27 1627 779 191092 20152 ffffffff b7f0d997 S com.android.calendar
root 1678 792 1148 508 c0269b33 b7f8c3ec S logcat
root 1755 792 1340 680 00000000 b7e66569 R /system/bin/sh
root 1760 1755 1648 556 00000000 b7f02e36 R ps

测试时,我总是确保我要查找的进程事先在列表中。在我看来,我的正则表达式应该可以工作,但也许我遗漏了一些重要的东西......

顺便说一句:我不想使用 grep 或 awk 等 shell 命令来提取 PID,因为我只能访问命令的结果,而不能访问 shell 本身。

最佳答案

您必须调用 Matcher.find():

final Matcher matcher = pattern.matcher(psResult);
matcher.find();
final String processPID = matcher.group(1);

关于Java Regex - 从 ps 命令中提取 PID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17925635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com