Python正则表达式搜索和分组

时间:2014-03-26 06:15:02

标签: python regex

04:38:06.151 [http-bio-5443-exec-8] INFO  A.g.r.q.r.flex.RubixService   - [NO-ID] [Mar 26, 2014 04:38:06 UTC] -     [5a-y2-24C363B223F1CB534B8DCA5693ADF2E9] --0->servAggregateMultiple:61224380032:[[{"responseMeasures":    ["SubscriberCnt","UpBytes","DownBytes","SessionCount","SubscriberPercentage","UpBytesPercentage","DownBytesPercentage","SessionCountPercentage"]
"sortProperty":"SubscriberCnt"

我在文本文件中有这些数据。我想执行搜索并找到responseMeasures,这是有大量数据。如果它找到了,我想在那之后存储所有内容: -

["SubscriberCnt","UpBytes","DownBytes","SessionCount","SubscriberPercentage","UpBytesPercentage","DownBytesPercentage","SessionCountPercentage"]

我想将它作为一个组存储在一个变量中。相似我希望找到sortProperty并将“SubscriberCnt”作为一个组存储在一个变量中。

这是我的代码段: -

import re
fo = open("data.txt", "r")
data = fo.readlines()
for n in data:
        matchMeasure = re.search("responseMeasures:", n)
        if matchMeasure:
                measureData =  matchMeasure.groups()
                print "Measures are" +  str(measureData)
        matchDimension = re.search("sortProperty", n)
        if matchSort:
                sortData = matchSort.groups()
                print "Group by" + str(sortData)

这段代码可能不是最好的方法,只是为了显示我要找的东西。

我想要的预期输出: -

Meausres are:-
SubscriberCnt
UpBytes
DownBytes
SessionCount
SubscriberPercentage
UpBytesPercentage
DownBytesPercentage
SessionCountPercentage"
Group by SubscriberCnt

1 个答案:

答案 0 :(得分:0)

您可以使用re.findall

来实现此目的
with open('data.txt','r') as f:

    first_filter = re.findall(r"[\w']+", f.read())
    measures = first_filter[first_filter.index('responseMeasures')+1:first_filter.index('sortProperty')]
    sortby = first_filter[first_filter.index('sortProperty')+1::]

    for measure in measures:
        print measure

    print "Group by:"

    for sort_type in sortby:
        print sort_type