[Baypiggies] regex puzzle
Aaron Maxwell
amax at redsymbol.net
Tue Mar 11 18:47:12 CET 2008
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
More information about the Baypiggies
mailing list