Why does \ w + match the trailing newline?

I am curious why the following deduces that there was a coincidence:

import re

foo = 'test\n'
match = re.search('^\w+$', foo)

if match == None:
  print "It did not match"
else:
  print "Match!"

A new line before the end of the line, yes? Why is this mapping?

+5
source share
2 answers

^and $mean “beginning of line” and “end of line”, not “beginning of line” and “end of line”. Use \Afor "start of line" and \Zfor "end of line."

+8
source

From the Python documentation re.

'$'
​​ , MULTILINE . foo "foo" "foobar", foo $ "foo". , foo. $ 'foo1\nfoo2\n' 'foo2 , ' foo1 MULTILINE; $ 'foo\n' () : .

+9

All Articles