[Baypiggies] regex puzzle

Matt Good matt at matt-good.net
Wed Mar 12 03:05:57 CET 2008

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

> 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.

I think what you're seeing is due to how the cmd module parses the  
command input.  It tries to behave like a Unix shell and treats \ as  
an escape character as described here:

It's easy to workaround this by overriding Cmd.onecmd and replacing  
the backslashes:

-- Matt

More information about the Baypiggies mailing list