Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I'm trying to match a string like 'r"abc"' or '"abc"' I thought I would do '[r|.]"[\w]+"'
'r"abc"'
'"abc"'
'[r|.]"[\w]+"'
Use ? to mean optional.
?
'r?"\\w+"'
Example usage:
>>> re.match('r?"\\w+"', 'r"abc"') <_sre.SRE_Match object at 0x0000000002A57440> >>> re.match('r?"\\w+"', '"abc"') <_sre.SRE_Match object at 0x00000000029FDAC0>
Add a comment
'r?"\w+"'
should do, you match r prefix optionally. Also square brackets around a \w are not needed: it's a single character!
r
\w
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.