Kann jemand erklären, warum Beispiel 1 unten funktioniert, wenn das rPräfix nicht verwendet wird? Ich dachte, das rPräfix muss verwendet werden, wenn Escape-Sequenzen verwendet werden. Beispiel 2 und Beispiel 3 zeigen dies.
# example 1
import re
print (re.sub('\s+', ' ', 'hello there there'))
# prints 'hello there there' - not expected as r prefix is not used
# example 2
import re
print (re.sub(r'(\b\w+)(\s+\1\b)+', r'\1', 'hello there there'))
# prints 'hello there' - as expected as r prefix is used
# example 3
import re
print (re.sub('(\b\w+)(\s+\1\b)+', '\1', 'hello there there'))
# prints 'hello there there' - as expected as r prefix is not used
'\s'(wier'\s') auch als dargestellt'\\s', da es sich'\s'nicht um eine erkannte Escape-Sequenz handelt.