[英]Python inconsistencies reading regex file
I have a regex file that I am reading in and compiling.我有一个正在阅读和编译的正则表达式文件。 The issue I am having is the regex will sometimes leading
\\
.我遇到的问题是正则表达式有时会导致
\\
。
Z\\d*\\.file_\\.?
instead of代替
Z\d*\.file_\.?
It sometimes matching but others not.它有时匹配,但有时不匹配。
Most likely it does not match when you use raw string and double backslash.当您使用原始字符串和双反斜杠时,它很可能不匹配。
s = "ABC 23"
re.findall('\d+',s)
['23']
re.findall(r'\d+',s)
['23']
re.findall('\\d+',s)
['23']
re.findall(r'\\d+',s)
[]
I don't know if that is what you want but if you read documentation of regular expression operations我不知道这是否是你想要的,但如果你阅读了正则表达式操作的文档
It says :它说 :
"Regular expressions use the backslash character ('\\') to indicate special forms or to allow special characters to be used without invoking their special meaning"
“正则表达式使用反斜杠字符 ('\\') 来表示特殊形式或允许使用特殊字符而不调用它们的特殊含义”
And also :并且 :
"The solution is to use Python's raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'"
“解决方案是对正则表达式模式使用 Python 的原始字符串表示法;在以 'r' 为前缀的字符串文字中,不会以任何特殊方式处理反斜杠”
Example:例子:
regex= re.compile(r'string')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.