转义正则表达式字符串 [英] Escaping regex string
问题描述
我想使用来自用户的输入作为搜索某些文本的正则表达式模式.它有效,但我如何处理用户将有意义的字符放入正则表达式的情况?
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?
比如用户要搜索Word(s)
:正则引擎会把(s)
作为一个组.我希望它把它当作一个字符串 "(s)"代码>.我可以在用户输入上运行
replace
并用 \(
和 )
替换 (
和 \)
但问题是我需要为每个可能的正则表达式符号进行替换.
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.
你知道更好的方法吗?
推荐答案
为此使用 re.escape()
函数:
转义(字符串)
返回所有非字母数字反斜杠的字符串;如果您想匹配可能包含正则表达式元字符的任意文字字符串,这将非常有用.
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.
一个简单的例子,搜索提供的字符串的任何出现,可选地后跟s",并返回匹配对象.
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)
这篇关于转义正则表达式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!