Raw strings and escaping

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

    Raw strings and escaping

    Hi,

    I would expect this to work,

    rawstring=r'som e things\new things\some other things\'

    But it fails as the last backslash escapes the single quote.

    ...although writing this I think I have solved my own problem. Is \' the
    only thing escaped in a raw string so you can place ' in a raw string?
    Although I thought the correct thing in that case would be;

    rawstring=r"raw string'with single-quote"


    This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email fromyour computer.

    You should not copy the email, use it for any purpose or disclose its contents to any other person.
    Please note that any views or opinions presented in this email may be personal to the author and do not necessarily represent the views or opinions ofDigica.
    It is the responsibility of the recipient to check this email for the presence of viruses. Digica accepts no liability for any damage caused by any virus transmitted by this email.

    UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
    Reception Tel: + 44 (0) 115 977 1177
    Support Centre: 0845 607 7070
    Fax: + 44 (0) 115 977 7000
    Digica | International AI powered software solutions company developing smart intelligent software solutions for Cloud, IoT and Embedded.


    SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South Africa
    Tel: + 27 (0) 21 957 4900
    Fax: + 27 (0) 21 948 3135
    Digica | International AI powered software solutions company developing smart intelligent software solutions for Cloud, IoT and Embedded.

  • Rob Williscroft

    #2
    Re: Raw strings and escaping

    Matthew Warren wrote in news:mailman.11 52.1159872720.1 0491.python-
    list@python.org in comp.lang.pytho n:
    I would expect this to work,
    >
    rawstring=r'som e things\new things\some other things\'
    It in the docs:

    <url:http://docs.python.org/ref/strings.html#l2 h-14>

    .... Specifically, a raw string cannot end in a single backslash (since the
    backslash would escape the following quote character). Note also that a
    single backslash followed by a newline is interpreted as those two
    characters as part of the string, not as a line continuation.

    Rob.

    Comment

    • Duncan Booth

      #3
      Re: Raw strings and escaping

      "Matthew Warren" <Matthew.Warren @Digica.comwrot e:
      I would expect this to work,
      >
      rawstring=r'som e things\new things\some other things\'
      >
      But it fails as the last backslash escapes the single quote.
      >
      ..although writing this I think I have solved my own problem. Is \'
      the only thing escaped in a raw string so you can place ' in a raw
      string? Although I thought the correct thing in that case would be;
      >
      rawstring=r"raw string'with single-quote"
      You cannot end *any* string literal with an odd number of backslash
      characters. The "r" prefix changes how the escape sequences are interpreted
      after the string literal has been parsed, it doesn't change how the literal
      itself is actually parsed. See the Python Reference Manual, 2.4.1:
      When an "r" or "R" prefix is present, a character following a
      backslash is included in the string without change, and all
      backslashes are left in the string. For example, the string literal
      r"\n" consists of two characters: a backslash and a lowercase "n".
      String quotes can be escaped with a backslash, but the backslash
      remains in the string; for example, r"\"" is a valid string literal
      consisting of two characters: a backslash and a double quote; r"\" is
      not a valid string literal (even a raw string cannot end in an odd
      number of backslashes). Specifically, a raw string cannot end in a
      single backslash (since the backslash would escape the following quote
      character). Note also that a single backslash followed by a newline is
      interpreted as those two characters as part of the string, not as a
      line continuation.

      Comment

      • Jon Ribbens

        #4
        Re: Raw strings and escaping

        In article <mailman.1152.1 159872720.10491 .python-list@python.org >, Matthew Warren wrote:
        I would expect this to work,
        >
        rawstring=r'som e things\new things\some other things\'
        >
        But it fails as the last backslash escapes the single quote.
        String constants in Python are weird - raw strings doubly so.
        In a raw string, backslashes are not special - unless followed by
        a quote character, when they simultaneously escape the quote and
        also insert a literal backslash.

        I presume there was originally some reason for this bizarre behaviour
        - it'd be interesting to know what it is/was, if anyone knows?

        Comment

        • Duncan Booth

          #5
          Re: Raw strings and escaping

          Jon Ribbens <jon+usenet@une quivocal.co.ukw rote:
          I presume there was originally some reason for this bizarre behaviour
          - it'd be interesting to know what it is/was, if anyone knows?
          >
          See the FAQ for the explanation:

          Contents: General Python FAQ- General Information- What is Python?, What is the Python Software Foundation?, Are there copyright restrictions on the use of Python?, Why was Python created in the fi...


          The idea is that if you use raw strings for regular expressions you can
          write things like:

          pattern = r'[\'"]'

          if the raw string simply ignored the backslash altogether it would be much
          harder to write regular expressions that contain both sorts of quotes.

          Comment

          • Jon Ribbens

            #6
            Re: Raw strings and escaping

            In article <Xns98518349247 01duncanbooth@1 27.0.0.1>, Duncan Booth wrote:
            >I presume there was originally some reason for this bizarre behaviour
            >- it'd be interesting to know what it is/was, if anyone knows?
            >>
            See the FAQ for the explanation:
            >
            Contents: General Python FAQ- General Information- What is Python?, What is the Python Software Foundation?, Are there copyright restrictions on the use of Python?, Why was Python created in the fi...

            >
            The idea is that if you use raw strings for regular expressions you can
            write things like:
            >
            pattern = r'[\'"]'
            >
            if the raw string simply ignored the backslash altogether it would be much
            harder to write regular expressions that contain both sorts of quotes.
            Well, hardly *much* harder:

            pattern = r"""foo"""

            Personally, I think that raw strings behaving as they do is
            unexpected, unintuitive and unnecessary, but it's obviously too late
            to change it now anyway ;-)

            I think standard strings accepting backslash followed by an unexpected
            character is also a mistake, but again it's too late to fix now.

            Comment

            • Duncan Booth

              #7
              Re: Raw strings and escaping

              Jon Ribbens <jon+usenet@une quivocal.co.ukw rote:
              Well, hardly *much* harder:
              >
              pattern = r"""foo"""
              It means you have to always triple quote your raw strings or know in
              advance of writing the regular expression which of r'', r"", r'''''',
              r"""""" is most appropriate. The way it works at the moment you don't have
              to care, you just write the raw string knowing that with r'' you have to
              escape single quotes, r"" you have to escape double quotes and everything
              works as expected.

              Its only when you start trying to use raw strings for things other than
              regular expressions that backslash at the end of the string can be a
              problem.

              Comment

              • Scott David Daniels

                #8
                Re: Raw strings and escaping

                Matthew Warren wrote:
                Hi,
                >
                I would expect this to work,
                >
                rawstring=r'som e things\new things\some other things\'
                >
                But it fails as the last backslash escapes the single quote.
                Note something many people don't when looking over the string rules:

                astring = r'some things\new things\some other things' '\\'

                gives you exactly what you want, doesn't imply a string concatenation
                at run time, and various other things. Two strings in succession are
                concatenated at source translation time.

                By the way, I renamed the result from being rawstring. It gives people
                bad intuitions to refer to some strings as "raw" when what you really
                mean is that the notation you are using is a "raw" notation for a
                perfectly normal string.

                --Scott David Daniels
                scott.daniels@a cm.org

                Comment

                Working...