How to use '\' in Python lookbehind assertion regex (?<=\\) to match C++-like quoted strings -


how can match r'\a' in python using lookbehind assertion?
actually, need match c++ strings "a \" b" ,

"str begin \ end" 

i tried:

>>> res = re.compile('(?<=\)a') traceback (most recent call last):   file "<stdin>", line 1, in <module>   file "/usr/lib/python2.7/re.py", line 190, in compile     return _compile(pattern, flags)   file "/usr/lib/python2.7/re.py", line 244, in _compile     raise error, v # invalid expression  >>> res = re.compile('(?<=\\)a') traceback (most recent call last):   file "<stdin>", line 1, in <module>   file "/usr/lib/python2.7/re.py", line 190, in compile     return _compile(pattern, flags)   file "/usr/lib/python2.7/re.py", line 244, in _compile     raise error, v # invalid expression sre_constants.error: unbalanced parenthesis  >>> res = re.compile('(?<=\\\)a') >>> ms = res.match(r'\a') >>> ms none true 

real example:
when i'm parcing "my s\"tr"; 5; ms = res.match(r'"my s\"tr"; 5;'), expected output is: "my s\"tr"

answer
stribizhev provided solution. thought initial regex less computationally expensive , issue should declared using raw string:

>>> res = re.compile(r'"([^\n"]|(?<=\\)["\n])*"', re.unicode) >>> ms = res.match(r'"my s\"tr"; 5;') >>> print ms.group() "my s\"tr" 

edit: final regex adaptation regex provided @ word aligned

i think looking regex:

(?s)"(?:[^"\\]|\\.)*" 

see demo on regex101.

sample python code (tested on tutorialspoint):

import re p = re.compile(ur'(?s)"(?:[^"\\]|\\.)*"') ms = p.match('"my s\\"tr"; 5;') print ms.group(0) 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -