我有一个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
在测试时,我总是确保我正在寻找的过程事先在列表中。在我看来,我的正则表达式应该可行,但也许我错过了一些重要的东西......
BTW :我不想使用像grep或awk这样的shell命令来提取PID,因为我只能访问命令的结果而不是shell本身。
答案 0 :(得分:3)
怎么样
Process p =
BufferedReader br=new BufferedReader(new InputStreamReader(ps.getInputStream()));
for(String line; (line = br.readLine()) != null;) {
if (line.endsWith(processName)) {
String pid = line.split(" +", -3)[1];
}
}
br.close();
答案 1 :(得分:3)
您必须调用Matcher.find()
:
final Matcher matcher = pattern.matcher(psResult);
matcher.find();
final String processPID = matcher.group(1);