如何以str格式返回(不打印)所有匹配的行? string是由\ t& s&分隔的长字符串。 \ n' S

时间:2014-07-21 12:54:36

标签: python regex

def Parser(string):
    string = string.split('\n')    
    import re
    for line in string:
        line = re.search(r"\S+\t+(\S+\t+)\S+\t+\S+\t+(\S+)\t+\S+", line)
        return line.group(1)+line.group(2)

这是我正在寻找的代码,终于得到了它。感谢提示 ...

def Parser(string):
string = string.split('\n')
firstline = string.pop(0)   
import re
matches = ''
for line in string:
    line = re.search(r"\S+\t+(\S+\t+)\S+\t+\S+\t+(\S+)\t+\S+", line)
    if line:    
        match = line.group(1) + line.group(2)+'\n'
        matches += match
return matches

2 个答案:

答案 0 :(得分:1)

假设您的其他代码(包括正则表达式)是正确的

def Parser(string):
    string = string.split('\n')    
    import re
    matches = []
    for line in string:
        line = re.search(r"\S+\t+(\S+\t+)\S+\t+\S+\t+(\S+)\t+\S+", line)
        match = line.group(1) + line.group(2)
        matches.extend(match)
    return matches

答案 1 :(得分:0)

考虑使用解析器进行输入。 Python附带csv模块:

import csv

def Parser(string):
    output = []

    for fields in csv.reader(string.split('\n'), 'excel-tab'):
        if len(fields) >= 6:
            output.append( fields[1] + '\t' + fields[4] )

    return output