2011-11-28 64 views
0

我试图让与命名组与正则表达式的URL部分地区的净提取到一个URL的命名组部分通过正则表达式

的例子是

/find/products/ 
/find/products/test/ 
/find/products/test/with/ 
/find/products/test/with/lids/ 
/find/products/test/page/3/ 
/find/products/test/with/lids/page/3/ 

从正则表达式的结果应该是

Query: Test 
Subset: Lids 
Page: 3 

或null取决于url,我想命名组,以便我可以稍后动态提取它。

我的尝试是

^/find/products/(?<Query>\w*)? 
(?<SubsQuery>/with/(?<Subset>\w*)?/)? 
(?<PageQuery>/page/(?<Page>\d)?/)? 
$ 

从例如

/find/products/ (matches) 
/find/products/test/ (doesnt) 
/find/products/test/with/ (doesnt) 
/find/products/test/with/lids/ (matches) 
/find/products/test/page/3/ (matches) 
/find/products/test/with/lids/page/3/ (doesnt) 

这意味着我失去了一些可选的东西?:(),但我似乎无法看到,我想有一天有太多的正则表达式:)

如果任何人都可以帮助我,将不胜感激。

回答

1

试试这个位置

Match result = Regex.Match(str, @"^/find/products/(?<Query>\w*)?/? 
    (?<SubsQuery>with/(?<Subset>\w*))?/? 
    (?<PageQuery>page/(?<Page>\d)?/)? 
    $", 
    RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); 

的问题是,你错过例如最后一个斜线“/ find/products/test /”,因为这是从下一个(不可用)组中覆盖的。

+0

这工作完美,我看起来很盲目:) – Sarkie

1

你的问题是你的正则表达式中有太多斜线(/)。也就是说,你在一个部分的末尾有一个,然后是下一个部分的开始。要解决这个问题最简单的方法是在每部分的末尾有斜杠:

^/find/products/(?<Query>\w*/)? 
(?<SubsQuery>with/(?<Subset>\w*/)?)? 
(?<PageQuery>page/(?<Page>\d/)?)? 
$ 

当然,这把斜线到您的命名组。为了消除它们,你需要更多的群体:

^/find/products/((?<Query>\w*)/)? 
(?<SubsQuery>with/((?<Subset>\w*)/)?)? 
(?<PageQuery>page/((?<Page>\d)/)?)? 
$ 
+0

我认为stema的回答稍微好一些,因为它没有没有名字的组,但是你的很有用,所以谢谢! – Sarkie