我正在尝试使用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,但由于我的复兴能力非常低,所以对我帮助不大。
更多样本输入:
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 :(得分: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 :(得分:1)
您可以使用
javascript:Add2ShopCart.*?,.*?,%20'(.*?)'.*?\$(\d+(?:\.\d+)?)
第1,2组捕获你想要的东西。
答案 2 :(得分: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']