Python re.match().groups()如何工作?

Python re.match().groups()如何工作?,python,regex,Python,Regex,我正在尝试使用re.match().groups()从字符串中删除所选信息: 我想要的结果是: ("Mortein%20Mouse%20Trap%201%20pack", "4.87") 所以我一直在尝试: re.match(r"(SEPARATOR)(SEPARATOR)", s).groups() #i.e.: re.match(r"(\',%20\')(\$)", s).groups() 我已经试着看了一遍,但由于我的调节技能太低,对我没有多大帮助 更多示例输入: javascript

我正在尝试使用re.match().groups()从字符串中删除所选信息:

我想要的结果是:

("Mortein%20Mouse%20Trap%201%20pack", "4.87")
所以我一直在尝试:

re.match(r"(SEPARATOR)(SEPARATOR)", s).groups() #i.e.:
re.match(r"(\',%20\')(\$)", s).groups()
我已经试着看了一遍,但由于我的调节技能太低,对我没有多大帮助

更多示例输入:

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

不是regex一次性的,希望它有帮助:

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']
In[16]:s=“”s=javascript:Add2ShopCart(document.OrderItemAddForm,%20'85575',%20'Mortein%20Mouse%20Trap%201%20pack',%20',%20'$4.87');”
[17]中:arr=s.split(“,%20”)
In[18]:arr[1]
输出[18]:“Mortein%20Mouse%20Trap%201%20pack”
在[19]:re.findall((?您可以使用

javascript:Add2ShopCart.*?,.*?,%20'(.*?)'.*?\$(\d+(?:\.\d+)?)
第1、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)
是匹配的整个字符串。

Moretein
之前没有括号。您有更多的示例输入和输出吗?@ExplosionPills我现在再添加一些您认为正则表达式的作用是什么?谢谢,有很多产品名称,所以我删除了“Mortein”,但在第一个前添加了一个额外的%20)很好,谢谢
re.search(r"'(Mortein.*?)'.*\$(.*?)'", s)