需要正则表达式格式-xx-xxx-x-


Need Regular Expression for Format -xx-xxx-x-

我需要从URL传递解析产品ID。URL的长度是不确定的,所以产品id。url可以是

http://<hostname>/p/Brand-Product-Name-6118-161-1-gallery.jpg

http://<hostname>/p/Brand-Product-Name-165-7128-12-gallery.jpg

我想要中间数值在任何情况下,即(161和7128)。我的编程语言是PHP

<?php
$url = "http://<hostname>/p/Brand-Product-Name-6118-161-1-gallery.jpg";
preg_match_all('#('d+)-('d+)-'d+-gallery'.jpg$#',$url,$matches);
// Depends on how accurate you want to be. You can go with many choices like:
// preg_match_all('#http'://.*?/p/.*?('d+)-('d+)-'d+-.*'.jpg#',$url,$matches);
var_dump($matches);
/*
array(3) {
  [0]=>
  array(1) {
    [0]=>
    string(22) "6118-161-1-gallery.jpg"
  }
  [1]=>
  array(1) {
    [0]=>
    string(4) "6118"
  }
  [2]=>
  array(1) {
    [0]=>
    string(3) "161"
  }
}
*/