[英]Python regex match escaped char
I need to match groups like: 我需要匹配以下组:
:data:
:abc'e12\:3\:text:
in a string, meaning 在字符串中,意思是
:(?P<data>.*?):
but to also allow escaped colon \\:
但也要允许逃逸的冒号
\\:
My best try looks like this: 我最好的尝试是这样的:
:(?P<data>((?<=\\):|.)*?):
p = re.compile(r':((\\:|[^:])+):')
print p.match(":abc'e12\:3\:text:").group(0)
You can always make sure you are getting the start and end characters with "^" and "$": 您始终可以确保使用“ ^”和“ $”作为起始字符和结束字符:
^:(.+):$
You can substitute ".+" with the type of regex you want too. 您也可以用所需的正则表达式类型替换“。+”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.