[Baypiggies] regex puzzle

Jason Culverhouse jason at mischievous.org
Wed Mar 12 01:28:57 CET 2008


Aaron,
I don't think that this helps you in python 2.5 but I think it  
achieves your goal
see http://pydoc.org/2.4.1/reconvert.html

import reconvert
rawstring =  eval(reconvert.quote(search_str))
regex = re.compile(rawstring)

Jason

On Mar 11, 2008, at 10:47 AM, Aaron Maxwell wrote:

> Hi all,
>
> I'm writing a tool with a command line interface, using the cmd  
> module.  It
> has a "log search" command, which accepts a regex pattern as an  
> argument.
>
> After tokenizing the input, I finally get what the user typed as the  
> regex
> pattern into a variable search_str:
>
>  regex = re.compile(search_str)
>  return [line for line in loglines if regex.search(line)]
>
> The problem is that search_str is a variable of type str, not a raw  
> string.
> So the user will have to escape many characters: e.g., "\\bREPO"  
> instead
> of "\bREPO" as the pattern.
>
> What I'd like to do is allow the user to type in the regular  
> expression
> directly, without them having to escape it.  Can someone suggest a  
> good
> solution?
>
> I could obviously have my code escape search_str, coding it to  
> manually
> replace each special character (http://docs.python.org/ref/strings.html 
> ).
> But that seems unpythonic (by which I mean "tedious and error  
> prone" :)
> There was a thread [0] that suggested using str.encode(), but I did  
> not find
> that to work - for example, with '\bRE'.encode('string_escape'), \b is
> interpreted as '\x08', the backspace character.
>
> Suggestions appreciated.
>
> Thanks,
> Aaron
>
> [0] http://mail.python.org/pipermail/python-list/2004-December/296389.html
>
> -- 
> Aaron Maxwell
> http://redsymbol.net
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies



More information about the Baypiggies mailing list