newb question on strings
omer at no-log.org
Wed Jun 25 15:02:07 CEST 2008
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
> I have a string called
> Some values of the string may contain a single quote as in -
> Frank's Laundry
> 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:
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)
You'll find a list of all the standard modules in the python docs, including
More information about the Python-list