regular expression

Danny Wong (dannwong) dannwong at cisco.com
Tue Aug 16 15:09:39 EDT 2011


Thanks chris. I had similar code to what you provided. I included the "#" (it was a comment in my code) as part of the string when it shouldn't be as part of my test. As soon as you pointed it out that the #'s aren't supposed to be part of the output, I removed them and it worked. How dumb of me. Thanks again.

-----Original Message-----
From: chris at rebertia.com [mailto:chris at rebertia.com] On Behalf Of Chris Rebert
Sent: Tuesday, August 16, 2011 12:26 AM
To: Danny Wong (dannwong)
Cc: python-list at python.org
Subject: Re: regular expression

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