Как отфильтровать несколько поданных с Python Регулярное выражение - PullRequest
0 голосов
/ 30 ноября 2018

Могу ли я узнать, как мне спроектировать фильтр, чтобы получить несколько совпадений для поля?Вот пример, мне нужно заполнить весь uplinkVolume из нижеследующего абзаца, чтобы суммировать весь объем восходящей линии связи.

Как это сделать с помощью регулярного выражения Python?

{extensionType: {1} длина: {48} serviceList: {: {serviceCode: {2000} uplinkVolume: {268266} downlinkVolume: {11761667} usageduration: {-} url: {-}зарядкиRuleBaseName: {-} ratingGroup: {-} serviceIdentifier: {-} localSequenceNumber: {-} envelopeStartTime: {-} envelopeEndTime: {-} duration: {-} changeTimeTimeZone: {-} noOCSCreditControl: {-} uplinkPacketNum: {-}downlinkPacketNum: {-}}: {serviceCode: {99} uplinkVolume: {296} downlinkVolume: {923} использованная длительность: {-} url: {-} ChargingRuleBaseName: {-} ratingGroup: {-} serviceIdentifier: {-} localSequenceNumber:{-} envelopeStartTime: {-} envelopeEndTime: {-} duration: {-} changeTimeTimeZone: {-} noOCSCreditControl: {-} uplinkPacketNum: {-} downlinkPacketNum: {-}}} changeTimeList: {-} recordOpeningTime: {-}длительность: {-} transparentVSA: {-} cdrType: {-} createTime: {-} зарядкиType: {-} роуминг: {-} профиль: {-} nsapi: {-} lastActivityTimeUpLink: {-} lastActivityTimeDownLink: {-}zoneId: {-} daylightSavingTime: {-} localTimeZone: {-} sgsnChange: {-} sessionID: {-} recordOpeningTimeZone: {-} saRecordChangeTime: {-} saRecordChangeTimeZone: {-} acctSessionId: {-} acctTerminateCause: {-}} {extensionType: {1} length: {144} serviceList: {: {serviceCode: {281} uplink4021} downlinkVolume: {4125} используемая длительность: {-} url: {-} ChargingRuleBaseName: {-} ratingGroup: {-} serviceIdentifier: {-} localSequenceNumber: {-} envelopeStartTime: {-} envelopeEndTime: {-} duration: {-} длительность: {-} changeTimeTimeZone: {-} noOCSCreditControl: {-} uplinkPacketNum: {-} downlinkPacketNum: {-}}: {serviceCode: {2000} uplinkVolume: {266097} downlinkVolume: {9213530} использованная продолжительность: {-} url: {-}зарядкиRuleBaseName: {-} ratingGroup: {-} serviceIdentifier: {-} localSequenceNumber: {-} envelopeStartTime: {-} envelopeEndTime: {-} duration: {-} changeTimeTimeZone: {-} noOCSCreditControl: {-} uplinkPacketNum: {-}downlinkPacketNum: {-}}: {serviceCode: {99} uplinkVolume: {1129} downlinkVolume: {2733} использованная длительность: {-} url: {-} ChargingRuleBaseName: {-} ratingGroup: {-} serviceIdentifier: {-} localSequenceNumber:{-} envelopeStartTime: {-} envelopeEndTime: {-} duration: {-} changeTimeTimeZone: {-} noOCSCreditControl: {-} uplinkPacketNum: {-} downlinkPacketNum: {-}}: {serviceCode: {281} uplinkVolume: {104} downlinkVolume:{135} usageduration: {-} url: {-} зарядкиRuleBaseName: {-} ratingGroup: {-} serviceIdentifier: {-} localSequenceNumber: {-} envelopeStartTime: {-} envelopeEndTime: {-} длительность: {-} changeTimeTimeZone:{-} noOCSCreditControl: {-} uplinkPacketNum: {-} downlinkPacketNum: {-}}: {serviceCode: {2000} uplinkVolume: {260058} downlinkVolume: {11145532} usageduration: {-} url: {-} зарядкиRuleBaseName: {-} ratingGroup: {-} serviceIdentifier: {-} localSequenceNumber: {-} envelopeStartTime: {-} envelopeEndTime: {-} duration: {-} changeTimeTimeZone: {-} noOCSCreditControl: {-} uplinkPacketNum: {-} downlinkPacketNum: {-}}: {serviceCode: {99} uplinkVolume: {294} downlinkVolume: {811} usageduration: {-} url: {-} ChargingRuleBaseName: {-} ratingGroup: {-} serviceIdentifier: {-} localSequenceNumber: {-} envelopeStartTime: {-} envelopeEndTime: {-} длительность: {-}changeTimeTimeZone: {-} noOCSCreditControl: {-} uplinkPacketNum: {-} downlinkPacketNum: {-}}} changeTimeList: {-} recordOpeningTime: {-} длительность: {-} transparentVSA: {-} cdrType: {-} createTime: {-} loadingType: {-} роуминг: {-} профиль: {-} nsapi: {-} lastActivityTimeUpLink: {-} lastActivityTimeDownLink: {-} zoneId: {-} daylightSavingTime: {-} localTimeZone: {-} sgsnChange: {-} sessionID: {-} recordOpeningTimeZone: {-} saRecordChangeTime: {-} saRecordChangeTimeZone: {-} acctSessionId: {-} acctTerminateCause: {-}}

Ответы [ 2 ]

0 голосов
/ 30 ноября 2018

Используйте это:

string = '{ extensionType:{1} length:{48} serviceList:{:{serviceCode:{2000} uplinkVolume:{268266} downlinkVolume:{11761667} usageduration:{-} url:{-} chargingRuleBaseName:{-} ratingGroup:{-} serviceIdentifier:{-} localSequenceNumber:{-} envelopeStartTime:{-} envelopeEndTime:{-} duration:{-} changeTimeTimeZone:{-} noOCSCreditControl:{-} uplinkPacketNum:{-} downlinkPacketNum:{-} } :{serviceCode:{99} uplinkVolume:{296} downlinkVolume:{923} usageduration:{-} url:{-} chargingRuleBaseName:{-} ratingGroup:{-} serviceIdentifier:{-} localSequenceNumber:{-} envelopeStartTime:{-} envelopeEndTime:{-} duration:{-} changeTimeTimeZone:{-} noOCSCreditControl:{-} uplinkPacketNum:{-} downlinkPacketNum:{-} } } changeTimeList:{-} recordOpeningTime:{-} duration:{-} transparentVSA:{-} cdrType:{-} createTime:{-} chargingType:{-} roaming:{-} profile:{-} nsapi:{-} lastActivityTimeUpLink:{-} lastActivityTimeDownLink:{-} zoneId:{-} daylightSavingTime:{-} localTimeZone:{-} sgsnChange:{-} sessionID:{-} recordOpeningTimeZone:{-} saRecordChangeTime:{-} saRecordChangeTimeZone:{-} acctSessionId:{-} acctTerminateCause:{-} } { extensionType:{1} length:{144} serviceList:{:{serviceCode:{281} uplinkVolume:{4021} downlinkVolume:{4125} usageduration:{-} url:{-} chargingRuleBaseName:{-} ratingGroup:{-} serviceIdentifier:{-} localSequenceNumber:{-} envelopeStartTime:{-} envelopeEndTime:{-} duration:{-} changeTimeTimeZone:{-} noOCSCreditControl:{-} uplinkPacketNum:{-} downlinkPacketNum:{-} } :{serviceCode:{2000} uplinkVolume:{266097} downlinkVolume:{9213530} usageduration:{-} url:{-} chargingRuleBaseName:{-} ratingGroup:{-} serviceIdentifier:{-} localSequenceNumber:{-} envelopeStartTime:{-} envelopeEndTime:{-} duration:{-} changeTimeTimeZone:{-} noOCSCreditControl:{-} uplinkPacketNum:{-} downlinkPacketNum:{-} } :{serviceCode:{99} uplinkVolume:{1129} downlinkVolume:{2733} usageduration:{-} url:{-} chargingRuleBaseName:{-} ratingGroup:{-} serviceIdentifier:{-} localSequenceNumber:{-} envelopeStartTime:{-} envelopeEndTime:{-} duration:{-} changeTimeTimeZone:{-} noOCSCreditControl:{-} uplinkPacketNum:{-} downlinkPacketNum:{-} } :{serviceCode:{281} uplinkVolume:{104} downlinkVolume:{135} usageduration:{-} url:{-} chargingRuleBaseName:{-} ratingGroup:{-} serviceIdentifier:{-} localSequenceNumber:{-} envelopeStartTime:{-} envelopeEndTime:{-} duration:{-} changeTimeTimeZone:{-} noOCSCreditControl:{-} uplinkPacketNum:{-} downlinkPacketNum:{-} } :{serviceCode:{2000} uplinkVolume:{260058} downlinkVolume:{11145532} usageduration:{-} url:{-} chargingRuleBaseName:{-} ratingGroup:{-} serviceIdentifier:{-} localSequenceNumber:{-} envelopeStartTime:{-} envelopeEndTime:{-} duration:{-} changeTimeTimeZone:{-} noOCSCreditControl:{-} uplinkPacketNum:{-} downlinkPacketNum:{-} } :{serviceCode:{99} uplinkVolume:{294} downlinkVolume:{811} usageduration:{-} url:{-} chargingRuleBaseName:{-} ratingGroup:{-} serviceIdentifier:{-} localSequenceNumber:{-} envelopeStartTime:{-} envelopeEndTime:{-} duration:{-} changeTimeTimeZone:{-} noOCSCreditControl:{-} uplinkPacketNum:{-} downlinkPacketNum:{-} } } changeTimeList:{-} recordOpeningTime:{-} duration:{-} transparentVSA:{-} cdrType:{-} createTime:{-} chargingType:{-} roaming:{-} profile:{-} nsapi:{-} lastActivityTimeUpLink:{-} lastActivityTimeDownLink:{-} zoneId:{-} daylightSavingTime:{-} localTimeZone:{-} sgsnChange:{-} sessionID:{-} recordOpeningTimeZone:{-} saRecordChangeTime:{-} saRecordChangeTimeZone:{-} acctSessionId:{-} acctTerminateCause:{-} }'

import re

regex = re.compile(r'uplinkVolume:{.*?}') # ? makes it not greedy and . will mach anything in curly brackets except new line
filtered_string = re.findall(regex, string)
print(filtered_string)

Выход:

C:\Users\Desktop>py x.py
['uplinkVolume:{268266}', 'uplinkVolume:{296}', 'uplinkVolume:{4021}', 'uplinkVolume:{266097}', 'uplinkVolume:{1129}', 'uplinkVolume:{104}', 'uplinkVolume:{260058}', 'uplinkVolume:{294}']
0 голосов
/ 30 ноября 2018

Это выражение находит все поля uplinkVolume и помещает значения найденных полей в группу.Значения, равные «-», в образец не включены.

r"uplinkVolume:{(\d+)}"

Пример использования:

import re

json_text = "YOUR_JSON_TEXT_FROM_THE_EXAMPLE_ABOVE"
field_values = re.findall(r"uplinkVolume:{(\d+)}", json_text)
# field_values = ['268266', '296', '4021', '266097', '1129', '104', '260058', '294']
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...