2014-10-16 112 views
0

我試圖創建一個類別頁面通用路由,正則表達式與NancyFx路線

我現在正則表達式進行評估,

\/[\w\/\-\%]+-c(\d+)(?:\/(\d{1,2}))?$ 

控制器代碼,

Get[@"\/[\w\/\-\%]+-c(?<id>\d+)(?:\/(?<page>\d{1,2}))?$", true] = async (x, ct) => 
{ 
    ... 
} 

我想與RegExr工具,它似乎是通過所有可能的排列組合和正則表達式,

i.e /anytgc123423/test/c123323/niec454stest/mystery-123-4534-thriller-suspense-c79/11 

Result: 
Group 1: 79 
Group 2: 11 

相同的正則表達式無法解析NancyFx Get函數中的示例中提供的url。

如果有人可以拋出一些光線,這將是很大的幫助。

在此先感謝。

回答

1

https://github.com/NancyFx/Nancy/wiki/Defining-routes

路由被拾起使用()正則表達式。

https://github.com/NancyFx/Nancy/blob/master/src/Nancy/Routing/Trie/TrieNodeFactory.cs#L38

 if (start == '(' && end == ')') 
     { 
      return new RegExNode(parent, segment, this); 
     } 

所以你的路線不會工作,除非你用括號括住它。

你的情況,你想用哪個用^()$

 if (segment.StartsWith("^(") && (segment.EndsWith(")") || segment.EndsWith(")$"))) 
     { 
      return new GreedyRegExCaptureNode(parent, segment, this); 
     } 
+0

感謝菲爾做了一個貪婪的正則表達式,我設法與貪婪的路線上班。 – 2014-10-18 13:25:32