简体   繁体   中英

Python Regex to match optionally double-quoted string

I want to match an optionally double-quoted string with regular expression using Python regex module re

The expression should give the following results:

"Assets". => Should Match

Assets. => Should Match

"Assets. => Shouldn't Match

Assets". => Shouldn't Match

I tried to achieve this using back reference in regular expression :

("?)Assets\1 

However, it matches even if there is no matching end quote. "Assets. -> neglects initial quote ", and matches the rest of the word.

What would be right expression for this ?

You can use the following pattern. Note that it basically lists the two separate cases because parentheses are notoriously not regular, but context-sensitive and, thus, difficult to handle with regular expressions:

>>> p = re.compile(r'^(?:"[^"]+"|[^"]+)$')
>>> bool(p.match('"assets"'))
True
>>> bool(p.match('"assets'))
False
>>> bool(p.match('assets'))
True

This also assumes that are no chars before or after the string that is being matched.

You regexp pattern is almost correct. You just have to make sure there are no quotes before and after your pattern. So use the pattern r'(?<!")("?)Assets\\1(?!")

>>> words = ['"Assets"', 'Assets', '"Assets', 'Assets"']
>>> ptrn = re.compile(r'(?<!")("?)Assets\1(?!")')
>>> [bool(ptrn.match(word)) for word in words]
[True, True, False, False]

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