0

I have a multiline string, like this:

tt = """
some text
myvar=foo
some more text
"""

Now what i want to do is to extract the value behind 'myvar=', in this case 'foo'.

I tried like this:

import re
m = re.match('^myvar=(.*)$', tt, flags=re.MULTILINE)
if m:
    print(m.group(1))

But mis always None. It only works with single line strings, like:

tt = 'myvar=foo'

What am I missing? I am new to python, but not to regex.

sotix
  • 822
  • 1
  • 8
  • 23
  • 1
    Use `re.search`. And you do not need `$`, remove it. – Wiktor Stribiżew Nov 24 '17 at 12:56
  • Wow thanks. That worked. Not very intuitive but ok. – sotix Nov 24 '17 at 12:58
  • Just read the docs very carefully: *The “`match`” operation succeeds only if the pattern matches at the start of the string **regardless of mode**, or at the starting position given by the optional pos argument regardless of whether a newline precedes it.* – Wiktor Stribiżew Nov 24 '17 at 12:59

0 Answers0