我對正則表達式非常陌生,所以我對這個問題的'noobyness'表示歉意...... 我需要爲我們在工作中使用的ID匹配模式。REGEX - 難以掌握基本知識
到目前爲止,該模式的唯一規範是它將有9個字符長,由大寫字母和數字組成。只要字符串的總長度爲9個字符,ID就可以包含1或任何數量的大寫字母或數字。
到目前爲止,我有以下... [AZ] [0-9] {9} 這並不能確保該字符串至少有一個字母或數字(因此一個9個字符的長字符串會通過)。 Alos,我確定它匹配了由非首都組成的9個字母的單詞。
我已經做了一些相當的谷歌搜索,但我還沒有發現任何愚蠢的東西足以讓我理解。
任何幫助非常apppreciated :)
感謝
編輯:只是爲了重溫要求 - 該ID必須爲9個字符,不能多也不能少。它將由大寫字母和數字組成。可以有任何數量的字母或數字,只要該ID包含至少一個(所以BH98T6YUO或R3DBLUEEE或1234R6789
我也會發布我的代碼,以確保位沒有錯......?
string myRegex = "A ton of different combinations that i have tried";
Regex re = new Regex(myRegex);
// stringCombos is a List<string> containing all my strings
// The strings contain within them, my id
// I am attempting to pull out this id
// the below is just to print out all found matches for each string in the list
foreach (string s in stringCombos)
{
MatchCollection mc = re.Matches(s);
Console.WriteLine("-------------------------");
Console.Write(s);
Console.WriteLine(" --- was split into the following:");
foreach (Match mt in mc)
{
Console.WriteLine(mt.ToString());
}
}
*我確定它匹配由非首字母組成的9個字母的單詞*確保您使用的是區分大小寫的搜索模式。 –
「包含1或任何數量的大寫字母或數字」和「字符串的總長度爲9個字符」聽起來有點矛盾。這是可變長度還是固定長度? – nhahtdh
您的模式實際上匹配一個大寫字母后跟9個數字...... –