c# - 分组匹配前一个元素零次或一次不捕获

标签 c# regex

我有以下网址:

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);

但是正则表达式的“匹配”方法并没有捕捉到它们中的任何一个。

最佳答案

您看到此行为是因为正则表达式的所有部分都是可选的。因此,它允许空匹配,这就是您在 URL 的“有效负载”部分加上一些其他文本前缀时得到的结果。

如果您通过在末尾添加 $ 将您的正则表达式“锚定”到 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);

Demo.

关于c# - 分组匹配前一个元素零次或一次不捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29467657/

相关文章:

regex - “the regular expression pattern is not valid”,同时用数组替换正则表达式

html - Dreamweaver 中的 RegEx 查找/替换 - 将 HTML 粘贴为变量?

c# - 我怎样才能防止 "possible loss of fraction"?

c# - 在内存数据缓存中提高 .Net 应用程序的性能

c# - 我的网络应用程序应该遵循什么设计模式?

java - 模式/正则表达式*仅*如果它是记录中的唯一字段

javascript - html5 模式不仅允许数字,还接受所有内容

c# - 添加 ActiveX 控件的 Interactive Brokers API 问题

c# - 文件流.ReadByte : Byte's are never negative numbers?

regex - 如何使用 R 或命令行提取与文本文件中的电子邮件地址匹配的表达式?