I wrote some simple test code to experiment with.
#begin code Python 2.7.12 running in Windows command window
import re
s2='''corn grows
higher\n
still.
'''
print (s2)
print (re.sub('\n', '~', s2),"test a")
print (re.sub(r'\n', '~', s2),"test ar")
print (re.sub('\s', '~', s2),"test b")
print (re.sub(r'\s', '~', s2),"test br")
##begin output to screen:####################
corn grows
higher
still.
('corn grows~higher~~ still.~', 'test a') ('corn grows~higher~~ still.~', 'test ar') ('corn~grows~higher~~~still.~', 'test b') ('corn~grows~higher~~~still.~', 'test br')
Why does the "r" for raw string not make a difference for this code?
When does that "r" not make a difference even tho there are special characters?
Is this really a topic that has been covered before? Come on. I looked.|
From the python docs, the re
module treats any escape sequence with single slash and double slash as the same. In other words '\\n'
and '\n'
are treated as the same in a string, along with '\\s'
and '\s'
, '\\r'
and '\r'
, etc.