[PYTHON] re.MULTILINE usage example

I wrote the same regular expression during this time and I don't understand why it didn't work.

remultiline.py


# -*- coding:utf-8 -*-
import re


text = """
[id][integer] PRIMARY KEY,
[column1][varchar](10) NOT NULL UNIQUE,
[column2][datetime] NULL,
[column3][decimal](10,2) NOT NULL
"""


pattern = r'^\[([^\]]+)\]\[([^\]]+)\](\([^\)]+\))?\s.+$'
#It will not be processed correctly without the MULTILINE flag.
print "with re.M", re.findall(pattern, text, re.M)
#=> [('id', 'integer', ''),
#    ('column1', 'varchar', '(10)'),
#    ('column2', 'datetime', ''),
#    ('column3', 'decimal', '(10,2)')]

print "without re.M", re.findall(pattern, text)
# => []

Recommended Posts

re.MULTILINE usage example
LINUX command [wc edition] Usage example
Keras usage example: Input (Embedding + Flatten)