有人可以帮我写一个正则表达式来修改下面的链接吗
mywebsite.com/images/products/12345678_pit.jpg<br>
mywebsite.com/images/products/12345678_piat_a2.jpg
链接:
mywebsite.com/images/zoomimage/12/12345678_var.jpg<br>
mywebsite.com/images/zoomimage/12/12345678_var_a2.jpg
我们看到了
products
= zoomimage
pit|piat
= var
12345678
= 12/12345678
(表示前两位数/初始数字)由于
答案 0 :(得分:1)
给你一个PHP示例,但正则表达式可以用另一种语言重用:
$urls = array(
'mywebsite.com/images/products/12345678_pit.jpg',
'mywebsite.com/images/products/23456789_foo.jpg',
'mywebsite.com/images/products/34567890_bar.jpg',
'mywebsite.com/images/products/12345678_piat_a2.jpg',
'mywebsite.com/images/products/45678901_baz_a2.jpg',
'mywebsite.com/images/products/56789012_qux_a2.jpg'
);
foreach($urls as $url) {
echo preg_replace(
'/products\/(\d{2})(\d*)_[a-z-]+(.*)\.jpg/i',
'zoomimage/$1/$1$2_var$3.jpg', $url) .'<br>';
}
// mywebsite.com/images/zoomimage/12/12345678_var.jpg
// mywebsite.com/images/zoomimage/23/23456789_var.jpg
// mywebsite.com/images/zoomimage/34/34567890_var.jpg
// mywebsite.com/images/zoomimage/12/12345678_var_a2.jpg
// mywebsite.com/images/zoomimage/45/45678901_var_a2.jpg
// mywebsite.com/images/zoomimage/56/56789012_var_a2.jpg