newb question on strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • regex_jedi

    newb question on strings

    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
  • shandy.b@gmail.com

    #2
    Re: newb question on strings

    Are you trying to escape for a regular expression?

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

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

    Generally, the module that consumes the string should provide a
    function like escape().


    On Jun 24, 1:27 pm, regex_jedi <kim.gen...@gma il.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

    Comment

    • Dan Bishop

      #3
      Re: newb question on strings

      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('Happ y')
      Happy
      >print re.escape("Fran k's Diner")
      >
      Frank\'s\ Diner
      >
      If you're escaping for URLs, there's urllib2.quote() , for a command
      line, use subprocess.list 2cmdline.
      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

      Comment

      • Bruno Desthuilliers

        #4
        Re: newb question on strings

        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('Happ y')
        >Happy
        >>>>print re.escape("Fran k's Diner")
        >Frank\'s\ Diner
        >>
        >If you're escaping for URLs, there's urllib2.quote() , for a command
        >line, use subprocess.list 2cmdline.
        >
        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.

        Comment

        • =?iso-8859-1?q?C=E9dric_Lucantis?=

          #5
          Re: newb question on strings

          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:


          Source code: Lib/re/ This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings ( str) as well as 8-...


          --
          Cédric Lucantis

          Comment

          Working...