0

我在 Python 中有以下代码

import psutil
import re

_list = psutil.get_pid_list()

for i in _list:
    if i > 0:
        _process_path = psutil.Process(i).exe
        _match = re.search('\/Applications\/.*\.app\/',_process_path)
        if _match:
            print _process_path

它有效,这是它返回的示例:

/Applications/RegExr.app/Contents/MacOS/RegExr
/Applications/Google Chrome.app/Contents/Versions/24.0.1312.57/Google Chrome Helper.app/Contents/MacOS/Google Chrome Helper
/Applications/TextWrangler.app/Contents/Helpers/Crash Reporter.app/Contents/Helpers/crash-catcher
/Applications/TextWrangler.app/Contents/MacOS/TextWrangler
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome
/Applications/Utilities/Activity Monitor.app/Contents/MacOS/Activity Monitor
/Applications/AppCleaner.app/Contents/Library/LoginItems/AppCleaner Helper.app/Contents/MacOS/AppCleaner Helper
/Applications/Transmit.app/Contents/MacOS/TransmitMenu.app/Contents/MacOS/TransmitMenu
/Applications/Twitter.app/Contents/MacOS/Twitter
/Applications/ScreenFlow.app/Contents/MacOS/ScreenFlowHelper.app/Contents/MacOS/ScreenFlowHelper

我怎样才能让它只返回这个呢?

/Applications/RegExr.app
/Applications/Google Chrome.app
/Applications/TextWrangler.app/Contents/Helpers/Crash Reporter.app
/Applications/TextWrangler.app
/Applications/Google Chrome.app
/Applications/Utilities/Activity Monitor.app
/Applications/AppCleaner.app
/Applications/Transmit.app
/Applications/Twitter.app
/Applications/ScreenFlow.app
4

2 回答 2

3

使用以下MatchObject.group()方法打印匹配的组:

print _match.group()
于 2013-02-18T13:01:00.793 回答
3

Your _match object can return the matched text as _match.group(0).

于 2013-02-18T13:01:52.477 回答