2017-02-13 18 views
2

成交全部Python RegExp從匹配字符串檢索值

我面臨一些不平凡的問題,我解析日誌。

我需要通過一個文件,並檢查行是否匹配模式: 如果是,那麼獲取此行中指定的ClientID。

行看起來像:

17.02.09 10:42:31.242 TRACE [1245] GDS:  someText(SomeText).ClientID: '' -> '99071901' 

所以我需要得到99071901.

我試圖構建正則表達式搜索模式,但它不是在「追蹤」 complete..stuck:

regex = '(^[(\d\.)]+) ([(\d\:)]+) ([\bTRACE\b]+) ([(\d)]+) ([\bGDS\b:)]+) ([\ClientID\b])' 

腳本代碼:

log=open('t.log','r') 
for i in log: 
    key=re.search(regex,i) 
    print(key.group()) #print string matching 
    for g in key: 
     client_id=re.seach(????,g) # find ClientIt  
log.close() 

欣賞如果你給我一個提示如何解決這個挑戰。

謝謝。

+1

如果你需要這些數字,我想你只需要'r''(\ d +)'$「'正則表達式並且抓住'.group(1)'。否則,要找到其他「子匹配」,「拼出」該模式,如['^([\ d。] +)\ s +([\ d。:] +)\ s +(TRACE)\ s + \ [[\ d +)] GDS:。*?ClientID:\ s *''\ s * - > \ s *'(\ d +)'$'](https://regex101.com/r/XVRXSx/1)並訪問您需要使用適當的索引來分組。 –

+0

Hi @WiktorStribiżew,如果要在循環中應用你的模式:regex =「^([\ d。] +)\ s +([\ d.:]+] \ s+(TRACE)\ s+\ [(\ d +)]] ?GDS:*客戶端ID:\ S * '' \ S * - > \ S * '(\ d +)' $「;我在日誌中:key = re.search(regex,i);如果密鑰不是無:ClientID = key.group(1);打印(客戶端ID); else:print('不匹配')。我沒有,沒有匹配 – AlexPes

+0

爲什麼循環?不,只要使用'm = re.search(pat,s)','如果m:print(m.group(n))'其中'n'是組ID。參見[Python演示](https://ideone.com/B74GUk)。 –

回答

1

您可以使用捕捉圍繞在你感興趣的圖案的部分括號,然後使用group(n)其中n是對應的組ID訪問這些部分:

import re 
s = "17.02.09 10:42:31.242 TRACE [1245] GDS:  someText(SomeText).ClientID: '' -> '99071901'" 
regex = r"^([\d.]+)\s+([\d.:]+)\s+(TRACE)\s+\[(\d+)] GDS:.*?ClientID:\s*''\s*->\s*'(\d+)'$" 
m = re.search(regex, s) 
if m: 
    print(m.group(1)) 
    print(m.group(2)) 
    print(m.group(3)) 
    print(m.group(4)) 
    print(m.group(5)) 

Python online demo

該圖案爲

^([\d.]+)\s+([\d.:]+)\s+(TRACE)\s+\[(\d+)] GDS:.*?ClientID:\s*''\s*->\s*'(\d+)'$ 

請參閱its online demo here

請注意,您已將組的字符類混淆:(...)組子模式並捕獲它們,而[...]定義匹配單個字符的字符類。

2

你不需要太具體。您可以捕捉這些部分並單獨解析它們。

讓我們開始,例如只需您的一個行:

line = "17.02.09 10:42:31.242 TRACE [1245] GDS:  someText(SomeText).ClientID: '' -> '99071901'" 

,然後讓添加得到所有的部分我們的第一個正則表達式:

import re 
line_regex = re.compile(r'(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+):\s+(.+)') 
# now extract each section 
date, time, level, thread, module, message = line_regex.match(line).groups() 

現在,如果我們看一下不同的部分他們將獲得我們需要做出更多決策或進一步解析的所有信息。現在讓我們在顯示正確的消息時獲取客戶端ID。

client_id_regex = re.compile(r".*ClientID: '' -> '(\d+)'") 

if 'ClientID' in message: 
    client_id = client_id_regex.match(message).group(1) 

現在我們有client_id


只需將該邏輯加入到您的循環中,並且您已完成設置。

line_regex = re.compile(r'(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+):\s+(.+)') 
client_id_regex = re.compile(r".*ClientID: '' -> '(\d+)'") 

with open('t.log','r') as f: # use with context manager to auto close the file 
    for line in f: # lets iterate over the lines 
     sections = line_regex.match(line) # make a match object for sections 
     if not sections: 
      continue # probably you want to handle this case 
     date, time, level, thread, module, message = sections.groups() 
     if 'ClientID' in message: # should we even look here for a client id? 
      client_id = client_id_regex.match(message).group(1) 
# now do what you wanted to do 
+0

HI Inbar玫瑰,非常感謝您的解決方案,以及它的作品 – AlexPes