I need a Perl-specific code in Python.
Here is the Perl code:
use strict;
use warnings;
my $s = 'http://www.bergdorfgoodman.com/Ippolita-18k-Gold-Rock-Candy-Mini-Single-Square-Pendant-Necklace/prod108010011/p.prod#.U75MVqY-PtS';
$s =~ s/(.*\.com)\/[^\/]+(\/prod[^\_]*\/p\.prod).*/$1$2/si;
print $s ."\n";
I have handled the capturing part by $1 and $2, and I don't know how to do it in Python.
Output:
http://www.bergdorfgoodman.com/prod108010011/p.prod
Here is the Python code:
import re
p = re.compile(ur'(.*\.com)\/[^\/]+(\/prod[^\_]*\/p\.prod).*', re.DOTALL | re.IGNORECASE)
test_str = u"http://www.bergdorfgoodman.com/Ippolita-18k-Gold-Rock-Candy-Mini-Single-Square-Pendant-Necklace/prod108010011/p.prod#.U75MVqY-PtS"
subst = ur"\1\2"
result = re.sub(p, subst, test_str)
In Python, you need to use \1
instead of $1
in the replacement string.
See regex demo and code demo on IDEONE
Output:
http://www.bergdorfgoodman.com/prod108010011/p.prod