quotes at the edges of a triple quote string...

  • Thread starter Anthony Roberts
  • Start date
A

Anthony Roberts

property = re.compile("""(?P<name>[a-z0-9_\-]+)="(?P<value>.*?)""""re.I)

This doesn't work because the closing quote in my regex forms the first
quote of a triple quote to end the string...

property = re.compile("""(?P<name>[a-z0-9_\-]+)=("(?P<value>.*?)")""", re.I)

This mucks up stuff I want to do later.

I've thought about it for a while, and except for changing it to a
single-quoted string with escaped quotes, is there any clean way to do this?

TIA,
Anthony
 
P

Peter Hansen

Anthony said:
property = re.compile("""(?P<name>[a-z0-9_\-]+)="(?P<value>.*?)""""re.I)

This doesn't work because the closing quote in my regex forms the first
quote of a triple quote to end the string...

property = re.compile("""(?P<name>[a-z0-9_\-]+)=("(?P<value>.*?)")""",
re.I)

This mucks up stuff I want to do later.

I've thought about it for a while, and except for changing it to a
single-quoted string with escaped quotes, is there any clean way to do
this?

Can't you still use a triple-quoted string, but escape the
last quote?
'test"'

-Peter
 
A

Anthony Roberts

Can't you still use a triple-quoted string, but escape the
last quote?

'test"'

Yes... yes I can. I can also use the other quotetation marks. I realized
this just after I posted.
'test"'

Either way works.

Thank you for the quick response. :)
 
S

Scott David Daniels

Anthony Roberts wrote (looking to fix):
>>> prop = re.compile("""(?P<name>[a-z0-9_\-]+)="(?P<value>.*?)""""re.I)
Yes... yes I can. I can also use the other quotetation marks. I realized
this just after I posted.

'test"'

A couple of things to realize (esp if you are getting into longer regexps).

1) r"""abc""" is a raw string with a triple quote.
2) Also, 'abc' 'def' (string splicing) is _exactly_ the same as 'abcdef'
The separate strings are glued together when converting the code to
bytecode, so there is no cost of concatenation to worry about.

What you might use for a regexp, then might be:

prop = re.compile(r"(?P<name>[a-z0-9_\-]+)="
r'"(?P<value>.*?)"')

The argument parens are enough for grouping, and the expression
broken at appropriate points is arguably more readable.

-Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,249
Messages
2,571,244
Members
47,876
Latest member
Kiptechie

Latest Threads

Top