2016-11-07 72 views
1

TODO:使用正則表達式擊穿驅動Python的正則表達式多組

drives = "8:20-24,30,31,32,10:20-24,30,31,32" 

最終輸出看起來就像這樣:

formatted_drives = [{8: [20,21,22,23,24,30,31,32]}, {10: [20,21,22,23,24,30,31,32]}] 

這裏是正則表達式現在的樣子:

regex_static_multiple_with_singles = re.match(r""" 
    (?P<enc>\d{1,3}):  # Enclosure ID: 
    (?P<start>\d+)   # Drive Start 
    -      # Range - 
    (?P<end>\d+)   # Drive End 
    (?P<singles>,\d+)+  # Drive Singles - todo resolve issue here 
    """, drives, (re.IGNORECASE | re.VERBOSE)) 

什麼是返回:

[DEBUG ] All Drive Sequences: ['8:20-24,30,31,32', '10:20-24,30,31,32'] 
[DEBUG ] Enclosure ID : 8 
[DEBUG ] Drive Start : 20 
[DEBUG ] Drive End  : 24 
[DEBUG ] Drive List : [20, 21, 22, 23, 24] 
[DEBUG ] Drive Singles : ,32 
[DEBUG ] Enclosure ID : 10 
[DEBUG ] Drive Start : 20 
[DEBUG ] Drive End  : 24 
[DEBUG ] Drive List : [20, 21, 22, 23, 24] 
[DEBUG ] Drive Singles : ,32 

問題是驅動器單曲只返回最後一組。在這種情況下,有3個單個驅動器,但是它是一個可變數量。返回所有單個驅動器的最佳方法是什麼?

+0

使用''和得到一個匹配之後,分割用'該值,'。 –

+0

您是否正在考慮使用PyPi正則表達式模塊? –

+0

謝謝,這是我需要的。只需使用're'模塊。 – JT1

回答

1

試試這個:

line = "8:20-24,30,31,32,10:21-24,30,31,32,15:11,12,13-14,16-18" 
regex = r"(\d+):((?:\d+[-,]|\d+$)+)" 

上述正則表達式會分裂每個塊基於:我們得到3個賽:

  1. 8:20-24,30,31,32 ,
  2. 10:21-24,30,31,32,
  3. 15:11,12,13-14,16-18

正則表達式2將分裂每個匹配成段

regex2 = r"\d+-\d+|\d+" 

爲匹配1,所述段是:

a)20-24 
b)30 
c)31 
d)32 

然後剩下的就是簡單和自explainatory在以下代碼:

#!/usr/bin/python 
import re 
regex = r"(\d+):((?:\d+[-,]|\d+$)+)" 
line = "8:20-24,30,31,32,10:21-24,30,31,32,15:11,12,13-14,16-18" 
regex2 = r"\d+-\d+|\d+" 

d={} 

matchObj = re.finditer(regex,line, re.MULTILINE) 

for matchNum, match in enumerate(matchObj): 
    #print (match.group(2)) 
    match2 = re.finditer(regex2,match.group(2)) 
    for matchNum1, m in enumerate(match2): 
     key=int(match.group(1)) 
     if '-' in m.group(): 
      y = m.group().split('-') 
      for i in xrange(int(y[0]),int(y[1])+1): 
       if key in d: 
        d[key].append(i) 
       else: 
        d[key] = [i,] 
     else: 
       if key in d: 
        d[key].append(int(m.group())) 
       else: 
        d[key] = [int(m.group()),]   
print(d)  

run the code here

示例輸出:(ΔP (?:\ d +)+)

{8: [20, 21, 22, 23, 24, 30, 31, 32], 10: [21, 22, 23, 24, 30, 31, 32], 15: [11, 12, 13, 14, 16, 17, 18]} 
+0

謝謝你,這非常靈活! – JT1