我有以下网址:
var = "/cars/bmw/x6/54d4190fcdc5900c78ef3bf6/postcode-rh69ta/100miles/min-1000/max-10000/under-10-years/under-100000-miles/automatic/hatchback/diesel";
每个url部分都是可选的,我想用以下正则表达式解析它们:
var rxUrlParser = new Regex(
@"(/postcode\-(?<postcode>\w+))?" + //postcode
@"(/(?<distance>\d+)miles)?" + //distance
@"(/min\-(?<minprice>\d+))?" + //minprice
@"(/max\-(?<maxprice>\d+))?" + //maxprice
@"(/(?<auo>under|over)-(?<age>\d+)-years)?" + //age
@"(/(?<muo>under|over)-(?<mileage>\d+)-miles)?" + //mileage
@"(/(?<trans>automatic|manual))?" + //transmission
@"(/(?<seller>trade|private))?" + //seller
@"(/(?<body>\b(HatchBack|Saloon|Estate|Coupe|Sports|Convertible|MPV|4[^A-Za-z0-9]*x[^A-Za-z0-9]*4|PickUp|Van)\b))?" + //bodytype
@"(/(?<fuel>\b(Petrol[^A-Za-z0-9]*Electric[^A-Za-z0-9]*Hybrid|Petrol[^A-Za-z0-9]*LPG[^A-Za-z0-9]*Hybrid|Diesel[^A-Za-z0-9]*Electric|Bioethanol|Petrol|Diesel|Electric|LPG)\b))?" + //fuel
@"(/(?<color>\b(Blue|Green|Brown|Red|Black|Beige|Pink|Yellow|Orange|White|Purple|Grey|Silver|Gold)\b))?" + //color
@"(/(?<sort>price\-max|price\-min|distance|mileage|newest))?" //sort
, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
但是&#34;匹配&#34;正则表达式的方法不捕获它们中的任何一个。
答案 0 :(得分:1)
您会看到此行为,因为正则表达式的所有部分都是可选的。因此,它允许一个空匹配,这是你在&#34;有效载荷前面得到的#34;带有其他文字的URL的一部分。
如果你&#34;锚定&#34;通过在结尾处添加$
将您的正则表达式添加到URL字符串的末尾,它将起作用:
var rxUrlParser = new Regex(
@"(/postcode\-(?<postcode>\w+))?" + //postcode
@"(/(?<distance>\d+)miles)?" + //distance
@"(/min\-(?<minprice>\d+))?" + //minprice
@"(/max\-(?<maxprice>\d+))?" + //maxprice
@"(/(?<auo>under|over)-(?<age>\d+)-years)?" + //age
@"(/(?<muo>under|over)-(?<mileage>\d+)-miles)?" + //mileage
@"(/(?<trans>automatic|manual))?" + //transmission
@"(/(?<seller>trade|private))?" + //seller
@"(/(?<body>\b(HatchBack|Saloon|Estate|Coupe|Sports|Convertible|MPV|4[^A-Za-z0-9]*x[^A-Za-z0-9]*4|PickUp|Van)\b))?" + //bodytype
@"(/(?<fuel>\b(Petrol[^A-Za-z0-9]*Electric[^A-Za-z0-9]*Hybrid|Petrol[^A-Za-z0-9]*LPG[^A-Za-z0-9]*Hybrid|Diesel[^A-Za-z0-9]*Electric|Bioethanol|Petrol|Diesel|Electric|LPG)\b))?" + //fuel
@"(/(?<color>\b(Blue|Green|Brown|Red|Black|Beige|Pink|Yellow|Orange|White|Purple|Grey|Silver|Gold)\b))?" + //color
@"(/(?<sort>price\-max|price\-min|distance|mileage|newest))?$" //sort
// Here is the only change --------^
, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);