简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM