1

我正在尝试使用 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 文档,但由于我的正则表达式技能太差,它对我没有多大帮助。

更多样本输入:

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');
4

3 回答 3

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)是匹配的整个字符串。

于 2013-02-13T15:49:51.317 回答
1

您可以使用

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

组 1,2 捕获您想要的。

于 2013-02-13T15:48:32.143 回答
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']
于 2013-02-13T15:46:03.037 回答