LoginSignup
0
0

More than 5 years have passed since last update.

re.MULTILINE 使用例

Posted at

この間これと同じ正規表現を書いてなぜ動かなかったのかわからない。

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.+$'
# MULTILINEフラグがないと正しく処理されない。
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)
# => []
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0