[Tutor] Issues with regex escaping on \{
vince spicer
vinces1979 at gmail.com
Wed Jul 29 21:18:04 CEST 2009
On Wed, Jul 29, 2009 at 11:35 AM, gpo <goodpotatoes at yahoo.com> wrote:
>
> My regex is being run in both Python v2.6 and v3.1
> For this example, I'll give one line. This lines will be read out of log
> files. I'm trying to get the GUID for the User ID to query a database with
> it, so I'd like a sub match. Here is the code
> -----------------
> import re
> line = '>Checking Privilege for UserId:
> {88F96ED2-D471-DE11-95B6-0050569E7C88}, PrivilegeId:
> {71AD2527-8494-4654-968D-FE61E9A6A9DF}. Returned hr = 0'
> pUserID=re.compile('UserID: \{(.+)\}',re.I) #Sub match is one or more
> characters between the first set of squigglies immediately following
> 'UserID: '
>
> #the output is:
> (re.search(pUserID,line)).group(1)
> '88F96ED2-D471-DE11-95B6-0050569E7C88}, PrivilegeId:
> {71AD2527-8494-4654-968D-FE61E9A6A9DF'
> -----------
> Why isn't the match terminating after it finds the first \} ?
> --
> View this message in context:
> http://www.nabble.com/Issues-with-regex-escaping-on-%5C%7B-tp24724060p24724060.html
> Sent from the Python - tutor mailing list archive at Nabble.com.
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
your grouping (.+) appears to be greedy, you can make it non-greedy with a
question mark
EX:
pUserID=re.compile('UserID:\s+{(.+?)}',re.I)
Vince
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090729/557bc037/attachment.htm>
More information about the Tutor
mailing list