Python正则表达式在一行文本中的关键字之前和之后获取n个字符

时间:2015-10-26 01:42:53

标签: python regex string file findall

我正在尝试解析文件并在字符串列表中搜索关键字。我需要在每次出现之前和之后返回'n'个字符。我没有正则表达式,但效率不高。知道怎么用正则表达式和findall做同样的事情?查找是一个字符串列表。这就是我没有正则表达式的原因:

with open(file, 'r') as temp:
    for num, line in enumerate(temp, 1):
        for string in lookup:
            if string in line:

                # Split the line in 2 substrings
                tmp1 = line.split(string)[0]
                tmp2 = line.split(string)[1]

                # Truncate only 'n' characters before and after the keyword
                tmp = tmp1[-n:] + string + tmp2[:n]

                # Do something here...

这是正则表达式的开始:

with open(file, 'r') as temp:
    for num, line in enumerate(temp, 1):
        for string in lookup:
            # Regex search with Ignorecase
            searchObj = re.findall(string, line, re.M | re.I)

            if searchObj:
                print "search --> : ", searchObj

                # Loop trough searchObj and get n characters 

2 个答案:

答案 0 :(得分:1)

来自https://docs.python.org/2/library/re.html

start([group])
end([group])
   Return the indices of the start and end of the substring matched by 
   group; group defaults to zero (meaning the whole matched substring). 
   Return -1 if group exists but did not contribute to the match. For a 
   match object m, and a group g that did contribute to the match, the 
   substring matched by group g (equivalent to m.group(g)) is


    m.string[m.start(g):m.end(g)]

    Note that m.start(group) will equal m.end(group) if group matched a 
    null string. For example, after m = re.search('b(c?)', 'cba'), 
    m.start(0) is 1, m.end(0) is 2, m.start(1) and m.end(1) are both    
    2, and m.start(2) raises an IndexError exception.

使用re.finditer,您可以生成MatchObject的迭代器,然后使用这些属性来获取子字符串的开头和结尾。

答案 1 :(得分:-1)

我得到了它的工作。如果有人需要,下面是代码:

with open(file, 'r') as temp:
    for num, line in enumerate(temp, 1):
        for string in lookup:

            # Regex
            searchObj = re.finditer(string, line, re.M | re.I)

            if searchObj:
                for match in searchObj:

                    # Find the start index of the keyword
                    start = match.span()[0]

                    # Find the end index of the keyword
                    end = match.span()[1]

                    # Truncate line to get only 'n' characters before and after the keyword
                    tmp = line[start-n:end+n] + '\n'            
                    print tmp