在Regex-Python中对输出模式进行分组

时间:2015-06-28 09:41:22

标签: python regex

我想只得到与正则表达式匹配的句子作为输出并避免这些NONE,我如何对与该模式匹配的输出进行分组?

import re regex = re.compile('(.*)(?:India)') with open("D:/txt_res/abc.txt") as f: for line in f: result = regex.search(line) print(result)

我得到的输出是

None
None
None
<_sre.SRE_Match object; span=(0, 101), match='Email: abc.bitz@gmail.com'>
None
None
None
<_sre.SRE_Match object; span=(0, 47), match='XYZ Engineer at ABC Organization, Bangalore'>
None
None
<_sre.SRE_Match object; span=(0, 32), match='Intern at S360, Bangalore'>
None
None

1 个答案:

答案 0 :(得分:0)

您可以测试None

import re
regex = re.compile('(.*)(?:India)')
with open("D:/txt_res/abc.txt") as f:
    for line in f:
        match = regex.search(line)
        if match is not None:
            print(match)