Python 3.7.4: 're.error: bad escape \s at position 0'

前端 未结 4 636
耶瑟儿~
耶瑟儿~ 2020-12-20 07:05

My program looks something like this:

import re
# Escape the string, in case it happens to have re metacharacters
my_str = \"The quick brown fox jumped\"
esc         


        
4条回答
  •  没有蜡笔的小新
    2020-12-20 07:26

    Regex engines behave the same way (mostly) when it comes to replacement strings
    that are handed to them.
    They try to insert the control code equivalent of escaped characters, like tabs crlf's, etc ...
    Any escape sequence it doesn't recognize, it just strips off the escape.

    Given
    spaced_pattern = re.sub(r"\\\s+", r"\s+", escaped_str)

    the r"\s+" hands the engine this replacement string \s+.
    Since there is no such escape sequence, it just strips off the escape
    and inserts s+ into the replace position.

    You can see it here https://regex101.com/r/42QCvi/1
    There is no error thrown, but it should be since your not getting what you think you should.

    In reality, a literal escape should always be escaped
    as can be seen here https://regex101.com/r/bzQgfN/1

    Nothing new, they just say its an error, but its really a notification warning
    that you're not getting what you think.
    Been this way for years and years. Sometimes its an error, sometimes not.

提交回复
热议问题