Reputation: 9381
I am trying to strip a selection of information from a string using re.match().groups():
s = "javascript:Add2ShopCart(document.OrderItemAddForm,%20'85575',%20'Mortein%20Mouse%20Trap%201%20pack',%20'',%20'$4.87');"
The result I want is:
("Mortein%20Mouse%20Trap%201%20pack", "4.87")
So I have been trying:
re.match(r"(SEPARATOR)(SEPARATOR)", s).groups() #i.e.:
re.match(r"(\',%20\')(\$)", s).groups()
I have tried looking at the re documentation, but as my regexing skills are so sub-par it's not helping me much.
More sample input:
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');
Upvotes: 1
Views: 631
Reputation: 191729
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)
This will return an array with the Mortein
and $
amounts as a tuple. You can also use:
re.search(r"'(Mortein.*?)'.*\$(.*?)'", s)
This returns a match. .group(1)
is Moretein
and .group(2)
is $
. .group(0)
is the entire string that was matched.
Upvotes: 2
Reputation: 32797
You can use
javascript:Add2ShopCart.*?,.*?,%20'(.*?)'.*?\$(\d+(?:\.\d+)?)
Group 1,2 captures what you want.
Upvotes: 1
Reputation: 195039
not a regex one shot, hope it helps:
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']
Upvotes: 0