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

newb question on strings

P: n/a
ok, I have looked a lot of places, and can't seem to get a clear
answer...

I have a string called
each_theme

Some values of the string may contain a single quote as in -
Happy
Sad
Nice
Frank's Laundry
Explosion

Notice that the 4th value has a single quote in it. Well, I need to
make sure that the single quote is escaped before handing it off for
further processing to a class I later call for some other processing.

So I thought, no big deal, I should be able to find a way to escape
the single quote on the string. I am a perl and PHP guy, so I do a
lot of regex stuff. I did a quick search and found someone had said
to use this re.sub function, so I tried. But the following doesn't
work. To be honest, I am a little lost with all the modules and
classes required to do simple math or string functions in Python.
None of it seems built it in.. its all import modules... Here is what
I am trying...

# escape single quotes in theme name
re.sub('''(['"])''', r'\\\1', each_theme)

you python masters... show me the way please...

thanks
regex_jedi
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies

P: n/a
Are you trying to escape for a regular expression?

Just do re.escape().
>>print re.escape('Happy')
Happy
>>print re.escape("Frank's Diner")
Frank\'s\ Diner

If you're escaping for URLs, there's urllib2.quote(), for a command
line, use subprocess.list2cmdline.

Generally, the module that consumes the string should provide a
function like escape().
On Jun 24, 1:27*pm, regex_jedi <kim.gen...@gmail.comwrote:
ok, I have looked a lot of places, and can't seem to get a clear
answer...

I have a string called
* *each_theme

Some values of the string may contain a single quote as in -
* *Happy
* *Sad
* *Nice
* *Frank's Laundry
* *Explosion

Notice that the 4th value has a single quote in it. Well, I need to
make sure that the single quote is escaped before handing it off for
further processing to a class I later call for some other processing.

So I thought, no big deal, I should be able to find a way to escape
the single quote on the string. *I am a perl and PHP guy, so I do a
lot of regex stuff. *I did a quick search and found someone had said
to use this re.sub function, so I tried. *But the following doesn't
work. To be honest, I am a little lost with all the modules and
classes required to do simple math or string functions in Python.
None of it seems built it in.. its all import modules... *Here is what
I am trying...

* * * * # escape single quotes in theme name
* * * * re.sub('''(['"])''', r'\\\1', each_theme)

you python masters... show me the way please...

thanks
regex_jedi
Jun 27 '08 #2

P: n/a
On Jun 24, 4:04*pm, "shand...@gmail.com" <shand...@gmail.comwrote:
Are you trying to escape for a regular expression?

Just do re.escape().
>print re.escape('Happy')
Happy
>print re.escape("Frank's Diner")

Frank\'s\ Diner

If you're escaping for URLs, there's urllib2.quote(), for a command
line, use subprocess.list2cmdline.
And if you're escaping for string literals in Python (or C and its
descendants), you can do:
>>print "Frank's Diner".encode('string-escape')
Frank\'s Diner

Jun 27 '08 #3

P: n/a
Dan Bishop a écrit :
On Jun 24, 4:04 pm, "shand...@gmail.com" <shand...@gmail.comwrote:
>Are you trying to escape for a regular expression?

Just do re.escape().
>>>>print re.escape('Happy')
Happy
>>>>print re.escape("Frank's Diner")
Frank\'s\ Diner

If you're escaping for URLs, there's urllib2.quote(), for a command
line, use subprocess.list2cmdline.

And if you're escaping for string literals in Python (or C and its
descendants), you can do:
>>>print "Frank's Diner".encode('string-escape')
Frank\'s Diner
And finally, if you're escaping it to use it for a SQL query, any DB-API
compliant adapter will automatically escape it's params if used correctly.
Jun 27 '08 #4

P: n/a
Le Tuesday 24 June 2008 22:27:33 regex_jedi, vous avez écrit*:
ok, I have looked a lot of places, and can't seem to get a clear
answer...

I have a string called
each_theme

Some values of the string may contain a single quote as in -
Happy
Sad
Nice
Frank's Laundry
Explosion

Notice that the 4th value has a single quote in it. Well, I need to
make sure that the single quote is escaped before handing it off for
further processing to a class I later call for some other processing.

So I thought, no big deal, I should be able to find a way to escape
the single quote on the string. I am a perl and PHP guy, so I do a
lot of regex stuff. I did a quick search and found someone had said
to use this re.sub function, so I tried. But the following doesn't
work. To be honest, I am a little lost with all the modules and
classes required to do simple math or string functions in Python.
None of it seems built it in.. its all import modules... Here is what
I am trying...

# escape single quotes in theme name
re.sub('''(['"])''', r'\\\1', each_theme)
No python has no builtin support for regexp like perl. There's nothing wrong
with your code, you just need to import the 're' module. Add this at the
beginning of your script:

import re

But imho escaping the quote in the first part would be more readable than
using triple quotes:
>>name = "Frank's Laundry"
re.sub(r"([\"'])", r"\\\1", name)
"Frank\\'s Laundry"

You'll find a list of all the standard modules in the python docs, including
this one:

http://docs.python.org/modindex.html
http://docs.python.org/lib/module-re.html

--
Cédric Lucantis
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.