Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: trouble with regex?

Reply
Thread Tools

Re: trouble with regex?

 
 
MRAB
Guest
Posts: n/a
 
      10-08-2009
inhahe wrote:
> Can someone tell me why this doesn't work?
>
> colorre = re.compile ('('
> '^'
> '|'
> '(?:'
> '\x0b(?:10|11|12|13|14|15|0\\d|\\d)'
> '(?:'
> ',(?:10|11|12|13|14|15|0\\d|\\d)'
> ')?'
> ')'
> ')(.*?)')
>
> I'm trying to extract mirc color codes.
>
> this works:
>
> colorre = re.compile ('\x0b(?:10|11|12|13|14|15|0\\d|\\d)'
> '(?:'
> ',(?:10|11|12|13|14|15|0\\d|\\d)'
> ')?'
> )
>
> but I wanted to modify it so that it returns me groups of (color code,
> text after the code), except for the first text at the beginning of the
> string before any color code, for which it should return ('', text).
> that's what the first paste above is trying to do, but it doesn't work.
> here are some results:
>
> >>> colorre.findall('a\x0b1,1')

> [('', ''), ('\x0b1,1', '')]
> >>> colorre.findall('a\x0b1,1b')

> [('', ''), ('\x0b1,1', '')]
> >>> colorre.findall('ab')

> [('', '')]
> >>> colorre.findall('\x0b1,1')

> [('', '')]
> >>> colorre.findall('\x0b1,1a')

> [('', '')]
> >>>

>
> i can easily work with the string that does work and just use group
> starting and ending positions, but i'm curious as to why i can't get it
> working teh way i want :/
>

The problem with the regex is that .*? is a lazy repeat: it'll try to
match as few characters as possible, which is why the second group is
always ''. Try a greedy repeat instead, but matching only
non-backspaces:

colorre = re.compile('('
'^'
'|'
'(?:'
'\x0b(?:10|11|12|13|14|15|0\\d|\\d)'
'(?:'
',(?:10|11|12|13|14|15|0\\d|\\d)'
')?'
')'
')([^\x0b]*)')
 
Reply With Quote
 
 
 
 
Paul McGuire
Guest
Posts: n/a
 
      10-08-2009
On Oct 8, 11:42*am, MRAB <(E-Mail Removed)> wrote:
> inhahe wrote:
> > Can someone tell me why this doesn't work?

>
> > colorre = re.compile ('('
> > * * * * * * * * * * * * '^'
> > * * * * * * * * * * * *'|'
> > * * * * * * * * * * * * '(?:'
> > * * * * * * * * * * * * * *'\x0b(?:10|11|12|13|14|15|0\\d|\\d)'
> > * * * * * * * * * * * * * *'(?:'
> > * * * * * * * * * * * * * * * ',(?:10|11|12|13|14|15|0\\d|\\d)'
> > * * * * * * * * * * * * * *')?'
> > * * * * * * * * * * * * ')'
> > * * * * * * * * * * * ')(.*?)')

>
> > I'm trying to extract mirc color codes.

>


You might find this site interesting (http://utilitymill.com/utility/
Regex_For_Range) to generate RE's for numeric ranges.

-- Paul
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i have no trouble to send , ihave trouble reciving mail --any ideas John Penney Computer Support 4 08-29-2006 08:45 PM
having trouble securing my wireless laptop FireBrick Wireless Networking 2 08-10-2004 12:37 PM
Trouble staying connected to wifi Michael Giroux Wireless Networking 1 08-03-2004 08:33 PM
Trouble connecting in public hot spots Andres Perez Wireless Networking 2 07-16-2004 06:47 PM
trouble with caching or caching the trouble Hypo ASP .Net 6 08-01-2003 07:11 AM



Advertisments