【问题标题】:Java Regex - Extract PID from ps commandJava Regex - 从 ps 命令中提取 PID
【发布时间】:2013-07-29 08:05:46
【问题描述】:

我有一个 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 本身。

【问题讨论】:

  • psResult 是什么,所有行一起还是单行?
  • 建议工具:Regexp Editor.
  • @Nishant 是所有的线条在一起!我刚刚尝试了以下两种解决方案,它们都有效。

标签: java regex shell


【解决方案1】:

怎么样

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

【讨论】:

  • 我认为psResult 代表所有行,而不仅仅是一个。
  • @NishantShreshth 在这种情况下,一次读取一行会更有效。
  • 可以获得多个匹配项。
  • 感谢您的回答!我一定会比较这两种解决方案,以找到更适合我的项目的解决方案。
  • 你可以通过执行ps -o pid,name来减少你的工作。只会打印这两列,现在您可以使用更简单的正则表达式,甚至可以使用拆分和数组引用。此外,它还减少了 IO。
【解决方案2】:

你必须调用Matcher.find():

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

【讨论】:

  • 哦,哇,我一直认为这是我的正则表达式......非常感谢! (看来我要等10分钟才能接受你的回答)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
  • 1970-01-01
  • 1970-01-01
  • 2013-03-23
  • 2011-12-28
  • 2018-07-17
  • 1970-01-01
相关资源
最近更新 更多