正则表达式以转义正则表达式

时间:2021-09-14 00:18:29

Yesterday, I needed to add a file path to a regular expression creating a pattern like this:

昨天,我需要为正则表达式添加一个文件路径,创建一个这样的模式:

"some_pattern/%s/more_pattern" % some_path

In the beginning the regular expression did not match, because some_path contained several regex specific symbols like ? or .. As a quick fix I replaced them with [?]{1} and . with \..

在开始时正则表达式不匹配,因为some_path包含几个正则表达式特定的符号,如?或者..作为快速修复,我用[?] {1}替换它们。用\ ..

However, I asked myself if there isn't a more reliable or better way to clean a string from regex specific symbols.

但是,我问自己是否没有更可靠或更好的方法来清除正则表达式特定符号中的字符串。

  • Is such functionality supported in the Python Standard library?
  • Python标准库是否支持此类功能?
  • If not, do you know a regular expression to identify all regex symbols and clean them through a substitute?
  • 如果没有,您是否知道正则表达式以识别所有正则表达式符号并通过替换符清除它们?

1 个解决方案

#1


6  

Just apply re.escape and you'll be fine.

只需应用re.escape就可以了。

re.escape(string)

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.

返回字符串,所有非字母数字反向;如果要匹配可能包含正则表达式元字符的任意文字字符串,这非常有用。

#1


6  

Just apply re.escape and you'll be fine.

只需应用re.escape就可以了。

re.escape(string)

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.

返回字符串,所有非字母数字反向;如果要匹配可能包含正则表达式元字符的任意文字字符串,这非常有用。