473,567 Members | 3,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
3 1968
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3220
by: Christoph Zwerschke | last post by:
I sometimes use triple quotes in order to produce snippets of multiline code, like that: if output == html: snip = '''<html> <head><title>Hello, World</title></head> <body bgcolor="aqua"><h1>What's up?</h1> </html>''' else: snip = 'Hello!'
11
2042
by: bearophile | last post by:
Hello, here are a four more questions (or suggestions) for the language (probably people have already discussed some of/all such things: I've seen the contracts for Python: http://www.wayforward.net/pycontract/ http://www.python.org/peps/pep-0316.html They look interesting and nice, how Python developers feel about accepting something...
4
3672
by: beliavsky | last post by:
The code for text in open("file.txt","r"): print text.replace("foo","bar") replaces 'foo' with 'bar' in a file, but how do I avoid changing text inside single or double quotes? For making changes to Python code, I would also like to avoid changing text in comments, either the '#' or '""" ... """' kind.
7
3959
by: Brian van den Broek | last post by:
Hi all, I'm posting partly so my problem and solution might be more easily found by google, and partly out of mere curiosity. I've just spent a frustrating bit of time figuring out why pydoc didn't extract a description from my module docstrings. Even though I had a well formed docstring (one line, followed by a blank line, followed by...
3
2525
by: Michele Simionato | last post by:
I am getting trouble with nested triple quoted strings in doctest. For instance $ cat x.py """ >>> dummy = ''' something here ''' """
14
6097
by: dmh2000 | last post by:
I recently complained elsewhere that Python doesn't have multiline comments. i was told to use triple quoted strings to make multiline comments. My question is that since a triple quoted string is actually a language construct, does it use cause a runtime construction of a string which is then discarded, or is the runtime smart enough to see...
1
3074
by: veaux | last post by:
I'm using Access query and have to spit out a text file to load into a 3rd party program. The text file has to include quotes (" ") around certain infomation. The string I have is below. CustomAttributes: "<xml><CustomAttribute Name=""Disenrollment Reason Code""' Value=" & "" & " DataType=""TXT"" /> My question is how do I include...
4
12726
by: Michael Yanowitz | last post by:
Hello: If I have a long string (such as a Python file). I search for a sub-string in that string and find it. Is there a way to determine if that found sub-string is inside single-quotes or double-quotes or not inside any quotes? If so how? Thanks in advance: Michael Yanowitz
7
4904
by: skunkwerk | last post by:
Hi, i'm trying to call subprocess.popen on the 'rename' function in linux. When I run the command from the shell, like so: rename -vn 's/\.htm$/\.html/' *.htm it works fine... however when I try to do it in python like so: p = subprocess.Popen(,stdout=subprocess.PIPE,stderr=subprocess.PIPE) print p.communicate()
0
7686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7650
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6272
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3648
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3630
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
930
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.