How do I get the raw representation of a string in Python?

后端 未结 1 615
南笙
南笙 2020-12-17 17:20

I am making a class that relies heavily on regular expressions.

Let\'s say my class looks like this:

class Example:
    def __init__(self, regex):
           


        
相关标签:
1条回答
  • 2020-12-17 17:54

    The problem with rawstring representation is, that you cannot represent everything in a portable (i.e. without using control characters) manner. For example, if you had a linebreak in your string, you had to literally break the string to the next line, because it cannot be represented as rawstring.

    That said, the actual way to get rawstring representation is what you already gave:

    "r'{}'".format(regex.pattern)
    

    The definition of rawstrings is that there are no rules applied except that they end at the quotation character they start with and that you can escape said quotation character using a backslash. Thus, for example, you cannot store the equivalent of a string like "\" in raw string representation (r"\" yields SyntaxError and r"\\" yields "\\\\").

    If you really want to do this, you should use a wrapper like:

    def rawstr(s):
        """
        Return the raw string representation (using r'') literals of the string
        *s* if it is available. If any invalid characters are encountered (or a
        string which cannot be represented as a rawstr), the default repr() result
        is returned.
        """
        if any(0 <= ord(ch) < 32 for ch in s):
            return repr(s)
    
        if (len(s) - len(s.rstrip("\\"))) % 2 == 1:
            return repr(s)
    
        pattern = "r'{0}'"
        if '"' in s:
            if "'" in s:
                return repr(s)
        elif "'" in s:
            pattern = 'r"{0}"'
    
        return pattern.format(s)
    

    Tests:

    >>> test1 = "\\"
    >>> test2 = "foobar \n"
    >>> test3 = r"a \valid rawstring"
    >>> test4 = "foo \\\\\\"
    >>> test5 = r"foo \\"
    >>> test6 = r"'"
    >>> test7 = r'"'
    >>> print(rawstr(test1))
    '\\'
    >>> print(rawstr(test2))
    'foobar \n'
    >>> print(rawstr(test3))
    r'a \valid rawstring'
    >>> print(rawstr(test4))
    'foo \\\\\\'
    >>> print(rawstr(test5))
    r'foo \\'
    >>> print(rawstr(test6))
    r"'"
    >>> print(rawstr(test7))
    r'"'
    
    0 讨论(0)
提交回复
热议问题