2013-04-12 32 views
0
var subject = "Parametre - Bloc Notes" 
var match = Regex.Match(subject, "(?i)(?:blah|para|foo).*?"); 
// This will work 
//"Para" doesn't match "Param" and it is before the dash 

var subject = "Parametre - Bloc Notes" 
var match = Regex.Match(subject, "(?i)(?:blah|blo|foo).*?"); 
// This will not work 
// "Blo" match "Bloc" and it is after the dash 

我認爲「 - 」,是我誤解的主要原因。正則表達式的匹配,破折號在主題字符串

編輯:

我真的很抱歉,所以我想正則表達式,以破折號前匹配帕拉姆,我該怎麼辦呢?

編輯2:

我承認我的問題是真的含糊不清,所以我的目標是要找到任何字符串Parametre字。

+1

什麼是真正的問題? – jmar777

+1

什麼是實際問題? –

+0

@BradM哈,打你5秒:) – jmar777

回答

1

要匹配-你可以簡單地做到這一點之前,任何詞來:

/(\w+)\s*\-/ 

,第一組將是「Parametre」在上面的例子中。例如,

"Foo - bar baz" // first group will be "Foo" 

"Hello - World" // first group will be "Hello" 

更新:我似乎誤解了你的問題。如果目的僅僅是爲了知道這個詞「Parametre」無論是否在一個字符串存在,你可以使用String.Contains

"Parametre - Bloc Notes".Contains("Parametre"); // true 

或者,如果您關注的是單詞邊界(即你想要匹配「Parametres」),你仍然可以使用正則表達式:

/\bParametre\b/ 
+0

謝謝你的幫助@ jmar777,我的不好,我提出的問題很模糊,我第二次編輯它。 – Ydhem

+1

這很難理解你要找的東西。如果您只想知道是否存在「參數」,則可以執行「參數 - 團塊註釋」.Contains(「Parametre」); // true' – jmar777