[Baypiggies] regex puzzle
Tony Cappellini
cappy2112 at gmail.com
Tue Mar 11 19:15:11 CET 2008
Take a look at Kodos.
It is a regular expression debugger, written in Python.
The project is in sourceforge
On Tue, Mar 11, 2008 at 10:47 AM, Aaron Maxwell <amax at redsymbol.net> 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