By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Anthony Roberts wrote:
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\""""

'test"'

-Peter
Jul 18 '05 #2

P: n/a
> Can't you still use a triple-quoted string, but escape the
last quote?
>>> """test\"""" 'test"'


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

'test"'

Either way works.

Thank you for the quick response. :)
Jul 18 '05 #3

P: n/a
Anthony Roberts wrote (looking to fix):
prop = re.compile("""(?P<name>[a-z0-9_\-]+)="(?P<value>.*?)""""re.I)
Peter Hansen wrote:
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"'''

'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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.