Matching multi line strings with regex with python

rocky shino
# your code goes here raw = ```

import re

raw = '''
bob = \'\'\'

 hello
\'\'\'


x = 2


sure = """ kjsfljwkel
 werwerwerwer
 """
'''


p = r"\w+\s*\=\s*('''|\"\"\").+\1"
print ('hello')
for m in re.finditer(p, raw,re.MULTILINE):
    print( m.group(0))

Basically its not working. You can see from the code what i'm trying to do. Thanks. I'm using python2 or python3, I dont think theres much difference in re.

blhsing

. matches non-newline characters by default, unless you enable the re.DOTALL (aliased re.S) flag, which allows it to match newline characters and therefore multiple lines that you desire. re.MULTILINE on the other hand is not needed here since you aren't using anchors such as ^ or $ in your regex pattern:

for m in re.finditer(p, raw, re.DOTALL):
    print(m.group(0))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related