2014-03-28 31 views
0

請有誰能幫助這個腳本,我是凡人而不是程序員!只有第一個字符串表達式工作查詢中有多個表達式/字符串

目的是爲了獲得訂單以下郵政編碼範圍將自己分配到文件夾 7000-8999 3000-3999 5000-5900

String AusPostcodeExp = @"^[78][0-9][0-9][0-9]$"; 
String AusPostcodeExp2 = @"^3[0-9][0-9][0-9]$"; 
String AusPostcodeExp3 = @"^5[0-8][0-9][0-9]$"; 
String AusPostcodeExp4 = @"^5[0-9][0][0]$"; 
String postcode = order.PostCode.ToString(); 

if (order.Country.ToUpper() == "AUSTRALIA" && IsValid(postcode, AusPostcodeExp)) 
if (order.Country.ToUpper() == "AUSTRALIA" && IsValid(postcode, AusPostcodeExp2)) 
if (order.Country.ToUpper() == "AUSTRALIA" && IsValid(postcode, AusPostcodeExp3)) 
if (order.Country.ToUpper() == "AUSTRALIA" && IsValid(postcode, AusPostcodeExp4)) 
{ 
order.SetAssignToAFolder("MELBOURNE"); 
+0

這是什麼語言?此外,你的代碼似乎缺少它的一部分。 –

+0

我很確定這是C#,但@Kendall是對的。如果這是C#,請使用包含'[C#]'標籤。 –

+0

就像我說的,我只是凡人,不知道你的意思是[c#]標籤! – jacquisu

回答

0

您可以使用交替(|)。例如,A|B將首先嚐試匹配模式A,否則將嘗試匹配模式B

您可以將其與其他正則表達式結構(如組)((…))和量詞({…})結合使用,以顯着簡化您的模式。

嘗試這樣的模式:

^([378][0-9]{3}|5([0-8][0-9]{2}|900))$ 

希望這個圖(使用Regexper生成)將有助於解釋什麼是怎麼回事:

regexper diagram

+0

這看起來是王牌,感謝你的幫助,但試過了,什麼也沒發生! \t String AusPostcodeExp = @「^([378] [0-9] {3} | 5([0-8] [0-9] {2} | 900))$」; String postcode = order.PostCode.ToString();如果(order.Country.ToUpper()==「AUSTRALIA」&& IsValid(postcode,AusPostcodeExp)) { order.SetAssignToAFolder(「MELBOURNE」);我知道我的原始代碼有點長,但第一行自己工作,它只是不喜歡它,當我添加其他範圍,我想也許我錯過了像'else'或應該刪除$簽署什麼? – jacquisu

+0

模式適合我。什麼是確切的輸入? ('postalcode') –

相關問題