regex - matching a multiline make-line variable assignment with a python regexp -
regex - matching a multiline make-line variable assignment with a python regexp -
i trying extract multiline make-line variable assignment multiline value. next testcase fails find match in input string , have confess fail see why. help on making sample code print "a \ b" on stdout welcome.
#!/usr/bin/env python def test(): s = r""" foo=a \ b """ import re print type(s),s regex = re.compile(r'^foo=(.+)(?<!\\)$', re.m) m = regex.search(s) print m.group(1) if __name__ == '__main__': test()
re.m means re.multiline, doesn't concern symbolism of dot, concerns symbolism of ^ , $
you need specify re.dotall create dot able match '\n'
def test(): s = r""" foo=a \ b """ import re print repr(s) print '---------------------' regex = re.compile(r'^foo=(.+)(?<!\\)$', re.m) print regex.search(s).group(1) print '---------------------' regex = re.compile(r'^foo=(.+)(?<!\\)$', re.m|re.dotall) print regex.search(s).group(1) test()
result
' \n\nfoo=a \\ \n\n b\n\n ' --------------------- \ ----- 'a \\ ' --------------------- \ b ----- 'a \\ \n\n b\n\n '
python regex multiline
Comments
Post a Comment