Monday, 15 August 2011

regex - How to get only the matched text in Python? -



regex - How to get only the matched text in Python? -

i have next code in python

import psutil import re _list = psutil.get_pid_list() in _list: if > 0: _process_path = psutil.process(i).exe _match = re.search('\/applications\/.*\.app\/',_process_path) if _match: print _process_path

it works , here illustration of returns:

/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

how create returns instead?

/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

print matched grouping instead, using matchobject.group() method:

print _match.group()

python regex

No comments:

Post a Comment