我对正则表达式很陌生,所以我为这个问题的“noobyness”道歉......我需要为我们在工作中使用的 ID 匹配一个模式。
到目前为止,该模式的唯一规范是它的长度为 9 个字符,并且由大写字母和数字组成。ID 可以包含 1 个或任意数量的大写字母或数字,只要字符串的总长度为 9 个字符。
到目前为止,我有以下... [AZ][0-9]{9} 这并不能确保字符串至少有一个字母或数字(因此 9 个字符长的字符串会通过)... Alos,我确定它匹配一个由非大写字母组成的 9 个字母的单词。
我已经做了一些谷歌搜索,但我没有发现任何足以让我理解的东西。
非常感谢任何帮助:)
谢谢
编辑:只是回顾一下要求 - 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());
}
}