2011-06-15 88 views
1

能否請你幫我寫的正則表達式這個幫助。正則表達式 - 請在形成這個表達式

NAME = 「Windows產品爲.NET」
TYPE = 「軟件編程」
數量= 「3包」

我希望做一個匹配這樣的在我需要RegEx的c#中。

If Name.contains(".Net") && (Type.Contains("Programming") || Type.Contains("Hardware") 
{ 
// output will be a Match. 
} 
else 
{ 
// no match. 
} 

我想在這裏拿,指定每個條件的正則表達式,然後應用邏輯操作數& &,邏輯分組paranthesis然後邏輯運算||這種方法。 我想出了這些所有的正則表達式。我怎樣才能提供邏輯操作數爲他們每個人以適當的順序執行?

string Name = "Windows Product for .Net"; 
    string Type = "Software Programming"; 

    string patternForName = ".*Net"; 
    Regex rgxName = new Regex(patternForName); 
    Match matchName = rgx.Match(Name); 
    string patternForType = ".*Programming"; 
    Regex rgxType = new Regex(patternForType); 
    Match matchType = rgx.Match(Type); 

    string patternForType1 = ".*Hardware"; 
    Regex rgxType1 = new Regex(patternForType1); 
    Match matchType1 = rgx.Match(Type); 

請注意 - 我們正在創造一種動態的,模式,操作數和正則表達式從XML文件來的感覺。所以這就是爲什麼我不想爲上面寫一個大的regEx。

+1

我不確定我是否遺漏了某些東西,但是從您的示例中可以看出,如果所有使用的都是.Contains,則不需要RegEx。你有更復雜的模式,你需要匹配? – mservidio 2011-06-15 16:23:10

回答

0

聽起來好像你在問你應該如何處理問題的邏輯部分。如果你從一個xml文件中取出它,你可以用你想要的邏輯結構來構建你的文件。

例如,已經和和或團體:

<And> 
    <Name Match=".Net"/> 
    <Or> 
     <Type Match="Programming"/> 
     <Type Match="Hardware"/> 
    </Or> 
</And> 

每個這些類型的創建類。爲了簡便起見,我沒有定義的類與屬性或創建構造函數,但你可以填寫出來,但是你想:

class YourType 
{ 
    string Name; 
    string Type; 
    string Quantity; 
} 

abstract class Test 
{ 
    public abstract bool RunTest(YourType o); 
} 

class AndTest : Test 
{ 
    public List<Test> Children; 
    public bool RunTest(YourType o) 
    { 
     foreach (var test in Children) 
     { 
     if (!test.RunTest(o)) return false; 
     } 
     return true; 
    } 
} 

class OrTest : Test 
{ 
    public List<Test> Children; 
    public bool RunTest(YourType o) 
    { 
     foreach (var test in Children) 
     { 
     if (test.RunTest(o)) return true; 
     } 
     return false; 
    } 
} 

class NameTest : Test 
{ 
    public string Match; 

    public bool RunTest(YourType o) 
    { 
     return o.Name.Contains(Match); 
    } 
} 

class TypeTest : Test 
{ 
    public string Match; 

    public bool RunTest(YourType o) 
    { 
     return o.Type.Contains(Match); 
    } 
} 

建立從XML文件中的類結構並調用的runTest從頂層測試。這樣你可以做任何你喜歡的邏輯。我只是使用Contains而不是正則表達式來簡化示例,但是您可以使用正則表達式匹配輕鬆替換字符串匹配。

+0

這非常有幫助。我可以構造一個通用機制:)。謝謝馬克。 – user476566 2011-06-15 18:31:55

0
if (rgxName.IsMatch(Name) && (rgxType.IsMatch(Type) || rgxType1.IsMatch(Type)) 
{ 
... 
} 
2

首先,除非你想整場比賽(即用火柴工作時),你不要在你的表達需要的領先.*。只是一個簡單的「是有」你不會需要它的模式可能匹配任何位置。

只需使用每個字段一個正則表達式(即一個Name,一爲Type,一個用於Quantity

string patternForName = "\\.Net"; // escaping the dot so it will match real dots only 
string patternForType = "Programming|Hardware"; // | will result in "left side or right side" 
string patternForQuantity = ".?"; // will match any string, even empty ones 

要檢查一切:

bool match = rgxName.IsMatch(Name) && rgxType.IsMatch(Type) && rgx.IsMatch(Quantity); 
0

在.NET中,正則表達式。匹配任何地方匹配的字符串,所以你不需要任何字符上你的模式前綴所以,檢查「.NET」,它只會是(*):

Regex regexName = new Regex(@"\.NET", RegexOptions.IgnoreCase); 
// IsMatch returns true/false, Match returns a Match object 
bool nameMatches = regexName.IsMatch(name); 

您的編程和硬件的模式也只是

new Regex(@"Programming", RegexOptions.IgnoreCase) // Or leave out IgnoreCase if you're case-sensitive 
new Regex(@"Hardware") 

如果你有名稱模式的列表和類型模式的列表,你可以做一些與此類似:

bool nameIsMatch = false; 
bool typeIsMatch = false; 

foreach (string namePattern in namePatterns) 
{ 
    nameIsMatch = nameIsMatch || Regex.IsMatch(nameString, namePattern); 
} 

foreach (string typePattern in typePatterns) 
{ 
    typeIsMatch = typeIsMatch || Regex.IsMatch(typeString, typePattern); 
} 

if (nameIsMatch && typeIsMatch) 
{ 
    // Whatever you want to do 
} 
1

你可以讓他們不動使用正則表達式。使用正則表達式不會真正節省您的任何時間和精力,因爲代碼將是差不多大小兩種方式。按照上面的圖案,你可以做這樣的事情:

var names = new[] { "Net", "Programming" }; 
var types = new[] { "Hardware" }; 

bool matchFound = true; 

foreach (string n in names) 
    matchFound &= Name.Contains(n); 

foreach (string t in types) 
    matchFound |= Type.Contains(t); 

上面的代碼假設你想匹配所有的「名稱」和任何「類型」的,但您可以替換任何你想要的邏輯。

你問題的真正癥結在於這些布爾組合;正則表達式不會幫助你理解這些邏輯,所以你最好使用string.Contains,除非你正在尋找的模式變得更加可變。在我看來,正則表達式正在分散你的真正目標。

0

patternForName = 「淨」 patternForType = 「編程」 patternForType1 = 「硬件」

您可能會發現正則表達式教練是有用的。