regular expression

Chris Rebert clp2 at rebertia.com
Tue Aug 16 09:26:21 CEST 2011


On Tue, Aug 16, 2011 at 12:00 AM, Danny Wong (dannwong)
<dannwong at cisco.com> wrote:
> Hi All,
>        If I get multiline standard output from a command. How can I
> retrieve this part of the string "(1006)"
> Example:
>
> #Committing...
> #Workspace: (1003) "My OS_8.12.0 Work" <-> (1004) "OS_8.12.0"
> #  Component: (1005) "he-Group" <-> (1004) "OS_8.12.0"
> #    Outgoing:
> #      Change sets:
> #        (1006)  *--@  <No comment>
> #          Changes:
> #            ---c- /he-Group/o-PI/target/config/common-ngp/makefile

Assuming the #s aren't in the actual output:

import re
pat = re.compile("^ *(\\([^)]+\\))", re.MULTILINE)
print(pat.search(your_str).group(1))

Obviously can vary depending on how you want to go about defining the
target string.

Cheers,
Chris

P.S. If you reply, please remove my email addresses from the quotation
due to "On Behalf Of" brokenness.
--
Freakin' Outlook/Exchange, I'm telling ya...
http://rebertia.com



More information about the Python-list mailing list