I want to use input from a user as a regex pattern for a search over some text. It works, but how I can handle cases where user puts characters that have meaning in regex? For example, the user wants to search for Word (s)
: regex engine will take the (s)
as a group. I want it to treat it like a string "(s)"
. I can run replace
on user input and replace the (
with \(
and the )
with \)
but the problem is I will need to do replace for every possible regex symbol. Do you know some better way ?
views:
1194answers:
2
+13
A:
Use the re.escape()
function for this:
escape(string)
Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.
A simplistic example, search any occurence of the provided string optionally followed by 's', and return the match object.
def simplistic_plural(word, text):
word_or_plural = re.escape(word) + 's?'
return re.match(word_or_plural, text)
ddaa
2008-11-11 09:37:37
+4
A:
You can use re.escape():
re.escape(string) Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.
>>> import re
>>> re.escape('^a.*$')
'\\^a\\.\\*\\$'
gimel
2008-11-11 09:49:25