Python – Regex, multiple match

I have a file which conatains amongst others SQL-CREATE-TABLE-commands. I want to write all SQL-CREATE-TABLE-commands into a list (not implemented yet), each command in a seperate list entry.

My problem is, that the regular expression does only return the first match, but there should be more.

Source file:

abcd
something
CREATE TABLE schema.test1(attribute1 DECIMAL(28, 7)  NULL ,
ATTRIBUTE2 DECIMAL(28, 7)  KEY  NOT NULL ,
ATTRIBUTE3 DECIMAL(28, 7)  NOT NULL ,
SET("db_alias_name" = 'TEST')
;

efgh
something else
CREATE TABLE schema.test2(attribute1 DECIMAL(28, 7)  NULL ,
ATTRIBUTE2 DECIMAL(28, 7)  KEY  NOT NULL ,
ATTRIBUTE3 DECIMAL(28, 7)  NOT NULL ,
SET("db_alias_name" = 'TEST')
;

something else
CREATE TABLE schema.test3(attribute1 DECIMAL(28, 7)  NULL ,
ATTRIBUTE2 DECIMAL(28, 7)  KEY  NOT NULL ,
ATTRIBUTE3 DECIMAL(28, 7)  NOT NULL ,
SET("db_alias_name" = 'TEST')
;
something else
12346
higkl

My script only returns the first match:

CREATE TABLE schema.test1(attribute1 DECIMAL(28, 7)  NULL ,
ATTRIBUTE2 DECIMAL(28, 7)  KEY  NOT NULL ,
ATTRIBUTE3 DECIMAL(28, 7)  NOT NULL ,
SET("db_alias_name" = 'TEST')

Script:

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

create_table_parts = []

atlfile = 'example.txt'
data = ''

def read_file(afile):
    with open(afile) as atl:
        text = atl.read()
        return text

data = read_file(atlfile)
data_utf8 = unicode(data, "utf-8")

round1 = re.search(r"(CREATEsTABLE).+?(?=;)", data_utf8, re.MULTILINE|re.DOTALL)
print round1.group()

Could you maybe tell me, what’s wrong here?

You’d be better off using finditer
because it returns a match
object like search
:

someIter = re.finditer(r"(CREATEsTABLE).+?(?=;)", data_utf8, re.MULTILINE|re.DOTALL)
for mObj in someIter:
    # process mObj
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Python – Regex, multiple match

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录