2013-09-20 237 views
1

我有一個問題,但我似乎無法弄清楚。我一直在referenceing這篇文章: Regular expression to match a line that doesn't contain a word?正則表達式匹配時不包含單詞

我想匹配的URL,如果URL中不包含2個破折號:

匹配= /test-doc.html

不匹配= /test-doc--help.html

我有這個工程匹配和不匹配:/(?<a>.(?!\-\-))*\.html

但是,組「a」只能得到1個字母與所有回頭看。我希望組「a」是「test-doc」,而不是最後的「c」。

任何建議,將不勝感激。

回答

2

嘗試這樣的圖案:

/(?<a>(?!.*--).*)\.html 

這將匹配文字/後面的零多個任意字符,捕獲在組a,(但僅如果該序列不包含文字-- ),後面是文字.html

例如:

Dim pattern As String = "/(?<a>(?!.*--).*).html" 
Regex.Match("/test-doc-help.html", pattern).Groups("a").Value // "test-doc-help" 
Regex.Match("/test-doc--help.html", pattern).Groups("a").Value // "" 
+0

神奇的表現。完美的作品。謝謝! – Chris

相關問題