1

The following piece of code does not match foo2 in the multiline string:

re.match("^foo2", "foo1\nfoo2\nfoo3",re.MULTILINE)

Why is that? The documentation at python.org says in MULTILINE mode the Caret (^) also matches at the beginning of each line. I've also tried the "raw string" versions with r already, no joy. Using findall also does not help. I'm obviously missing something here..

daniel.wirtz
  • 980
  • 7
  • 11

1 Answers1

3

See re.match documentation:

Note that even in MULTILINE mode, re.match() will only match at the beginning of the string and not at the beginning of each line.

You need to use re.search.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • It is interesting that the original post does not contain this line from the documentation. – Wiktor Stribiżew Aug 21 '15 at 07:19
  • interesting? :-) just me being stupid not to read everything.. on the other way round, i think it'll help if the python docs mentioned that `match/search` issue at the explanation of `'$'` – daniel.wirtz Aug 26 '15 at 17:34
  • Please do not call yourself names, we are just humans, we cannot remember everything. And a tip: when looking for something on SO, use tags in brackets and type some keyword of your problem: `[python][regex] match` - and it will surely give you a lot of interesting posts. – Wiktor Stribiżew Aug 26 '15 at 18:24