convert string to raw string?

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

    #1

    convert string to raw string?

    Hi,

    I'm writing a regex related program that lets the user supplies the
    regex definition. Is there an easy way to convert a string into a raw
    string?


    Cheers
  • Terry Reedy

    #2
    Re: convert string to raw string?


    "Phd" <pyjunk@shaw.ca > wrote in message
    news:39Usd.4392 57$nl.135440@pd 7tw3no...[color=blue]
    > Hi,
    >
    > I'm writing a regex related program that lets the user supplies the regex
    > definition. Is there an easy way to convert a string into a raw string?[/color]

    There ain't no such thing as raw strings. Just 'raw' string *literals*,
    where 'raw' denotes a mode for converting the literal in the code (which is
    *not* a Python string) into a Python string.

    Perhaps what you want is something like eval("r'%s'" % (raw_input(),)) ,
    assuming no ' or " in user input.

    Terry J. Reedy



    Comment

    • Duncan Booth

      #3
      Re: convert string to raw string?

      Phd wrote:[color=blue]
      > I'm writing a regex related program that lets the user supplies the
      > regex definition. Is there an easy way to convert a string into a raw
      > string?[/color]

      A raw string is a feature of the syntax of Python. Python gives you several
      ways to write strings: single quoted, double quoted, triple single, triple
      double, and raw versions of these. Once parsed from the source all of these
      simply become objects of type 'str'.

      The unicode modifier on a string does produce a different type of object
      internally (type 'unicode'), but the raw modifier does not.

      So, perhaps you would like to clarify what you really want to do? Perhaps
      give an example?

      If you are getting input from the user, then unless you are doing some
      processing on it to interpret escape sequences the chances are you already
      have what you need to use as a regular expression. If you *are*
      interpreting escape sequences then the answer is you need to not do that
      since the operation isn't really reversible.

      Comment

      • Steven Bethard

        #4
        Re: convert string to raw string?

        Duncan Booth wrote:[color=blue]
        > If you are getting input from the user, then unless you are doing some
        > processing on it to interpret escape sequences the chances are you already
        > have what you need to use as a regular expression. If you *are*
        > interpreting escape sequences then the answer is you need to not do that
        > since the operation isn't really reversible.[/color]

        If you are interpreting escape sequences, you probably want
        string.decode or string.encode:
        [color=blue][color=green][color=darkred]
        >>> '\\n\\r'.decode ('string_escape ')[/color][/color][/color]
        '\n\r'[color=blue][color=green][color=darkred]
        >>> '\n\r'.encode(' string_escape')[/color][/color][/color]
        '\\n\\r'

        Steve

        Comment

        Working...