我在下面的re.compile函数中遇到了一些困惑,我知道它会编译来检测所有不可打印的字符。但是我不确定放在compile函数中的参数的含义。谢谢你们!
re_print = re.compile('[^%s]' % re.escape(string.printable))
发布于 2019-04-29 02:15:05
分解一下看看有没有帮助。在python3解释器中运行以下代码:
import string
import re
# This will be the contents of the variable referenced
print(string.printable)
# This is what happens after all those characters are escaped by re
print(re.escape(string.printable)
# This is the whole value you are giving to re.compile (the re_print):
print('[^%s]' % re.escape(string.printable))
# Note the ^ in front means anything NOT printables
re_print可能用于检查某些文本中的不可打印字符(不是在string.printable中),但其中一些字符需要进行转义,否则将无法获得预期结果,因为特殊字符可能会被解释为正则表达式语句。
https://stackoverflow.com/questions/55896085
复制