2013-02-13 22 views
1

我想用re.match()從字符串中去除選擇的信息組():re.match()。groups()如何工作?

s = "javascript:Add2ShopCart(document.OrderItemAddForm,%20'85575',%20'Mortein%20Mouse%20Trap%201%20pack',%20'',%20'$4.87');" 

我想要的結果是:

("Mortein%20Mouse%20Trap%201%20pack", "4.87") 

所以我一直在努力:

re.match(r"(SEPARATOR)(SEPARATOR)", s).groups() #i.e.: 
re.match(r"(\',%20\')(\$)", s).groups() 

我已經試過看re documentation,但我regexing技能是如此低於標準桿它沒有幫助我很多。

更多樣本輸入:

javascript:Add2ShopCart(document.OrderItemAddForm,%20'85575',%20'Mortein%20Mouse%20Trap%201%20pack',%20'',%20'$4.87'); 

javascript:Add2ShopCart(document.OrderItemAddForm_0,%20'85575',%20'Mortein%20Mouse%20Trap%201%20pack',%20'',%20'$4.87'); 

javascript:Add2ShopCart(document.OrderItemAddForm,%20'8234551',%20'Mortein%20Naturgard%20Fly%20Spray%20Eucalyptus%20320g',%20'',%20'$7.58'); 

javascript:Add2ShopCart(document.OrderItemAddForm,%20'4204369',%20'Mortein%20Naturgard%20Insect%20Killer%20Automatic%20Outdoor%20Refill%20152g',%20'',%20'$15.18'); 

javascript:Add2ShopCart(document.OrderItemAddForm_0,%20'4204369',%20'Mortein%20Naturgard%20Insect%20Killer%20Automatic%20Outdoor%20Refill%20152g',%20'',%20'$15.18'); 

javascript:Add2ShopCart(document.OrderItemAddForm,%20'4220523',%20'Mortein%20Naturgard%20Outdoor%20Automatic%20Prime%201%20pack',%20'',%20'$32.54'); 
+0

'Moretein'前面沒有括號。你有更多的示例輸入和輸出嗎? – 2013-02-13 15:36:32

+0

@ExplosionPills我現在會添加更多 – jsj 2013-02-13 15:40:26

+0

你認爲你的正則表達式有什麼作用? – phant0m 2013-02-13 15:43:10

回答

2
re.findall(r""" 
    '   #apostrophe before the string Mortein 
    (   #start capture 
    Mortein.*? #the string Moretein plus everything until... 
    )   #end capture 
    '   #...another apostrophe 
    .*   #zero or more characters 
    \$   #the literal dollar sign 
    (   #start capture 
    .*?  #zero or more characters until... 
    )   #end capture 
    '   #an apostrophe""", s, re.X) 

這將返回與Mortein$量爲元組的陣列。您還可以使用:

re.search(r"'(Mortein.*?)'.*\$(.*?)'", s) 

這會返回一個匹配項。 .group(1)Moretein.group(2)$.group(0)是匹配的整個字符串。

+1

謝謝,有很多產品名稱,所以我刪除了「Mortein」,但在第一個之前添加了額外的%20( - 非常感謝 – jsj 2013-02-13 15:57:33

0

不是一個正則表達式進行一次拍攝,希望它有助於:

In [16]: s="""s = javascript:Add2ShopCart(document.OrderItemAddForm,%20'85575',%20'Mortein%20Mouse%20Trap%201%20pack',%20'',%20'$4.87');""" 

In [17]: arr=s.split("',%20'") 

In [18]: arr[1] 
Out[18]: 'Mortein%20Mouse%20Trap%201%20pack' 

In [19]: re.findall("(?<=\$)[^']*",arr[3]) 
Out[19]: ['4.87'] 
1

您可以使用

javascript:Add2ShopCart.*?,.*?,%20'(.*?)'.*?\$(\d+(?:\.\d+)?) 

組1,2捕捉你想要什麼。