1

I'm trying to match a string like 'r"abc"' or '"abc"' I thought I would do '[r|.]"[\w]+"'

0

2 Answers 2

3

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> 
Sign up to request clarification or add additional context in comments.

Comments

1
'r?"\w+"' 

should do, you match r prefix optionally. Also square brackets around a \w are not needed: it's a single character!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.