[Tutor] Regex ^$ not behaving as expected

Edmund Butterworth edmundb at talktalk.net
Thu Dec 8 07:45:31 EST 2016


Hello,

I am new to Python and trying to get to grips with the re regex module. 
I’m running Python 3.4 under Debian Jessie with a Cinnamon desktop.

My understanding is that when the re.M flag is raised, |^| will match at 
the beginning of the string and also at the beginning of each line 
within the string immediately following each newline \n. Similarly, |$ 
|matches both at the end of the whole string and at the end of each line 
immediately preceding the \n.

However I don’t seem to be able to get re.sub (for instance) to behave 
in this way. Both ^ and $ seem only to match the beginning and end of 
the string and not the individual lines within it. The session below 
hopefully demonstrates my confusion.


Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "copyright", "credits" or "license()" for more information.

 >>> import re
 >>> s = "AAAcBBB\nAAAdBBB"
 >>> print(s)
AAAcBBB
AAAdBBB
 >>> print(re.sub(r'^.+?c', "X", s))
XBBB
AAAdBBB
 >>> print(re.sub(r'^.+?d', "X", s, re.M))
AAAcBBB
AAAdBBB
 >>> print(re.sub(r'^.+?d', "X", s, re.M, re.S))
XBBB
 >>> print(re.sub(r'd.+?$', "X", s))
AAAcBBB
AAAX
 >>> print(re.sub(r'c.+?$', "X", s, re.M))
AAAcBBB
AAAdBBB
 >>> print(re.sub(r'c.+?$', "X", s, re.M, re.S))
AAAX
 >>>

What am I doing wrong? Thank you

Ed






More information about the Tutor mailing list