我需要一个常规的快递:字母数字格式。例如:"E7R8R9"。下面是我的代码:
string txt = "G1R1A3";
// Any Single Word Character (Not Whitespace) 1
string re1 = "[a-z][0-9][a-z][0-9][a-z][0-9]";
Regex r = new Regex(re1, RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = r.Match(txt);
if (m.Success)
{
    String w1 = m.Groups[1].ToString();
    Console.Write("(" + w1.ToString() + ")" + "\n");
}
Console.ReadLine();但这段代码也与"GG1R1A3"匹配。请帮帮忙。
发布于 2011-05-05 19:47:41
您的代码将在字符串中的任何位置搜索该模式。如果要将其锚定到字符串的开头(和结尾),请使用^和$
    string re1 = "^[a-z][0-9][a-z][0-9][a-z][0-9]$";发布于 2011-05-05 19:52:15
您可以以更短的方式使用这个正则表达式
       string strRegex = @"^([A-Z]\d){3}$";
        Regex myRegex = new Regex(strRegex);
        string strTargetString = @"E7R8R9";
        Match myMatch = myRegex.Match(strTargetString);
            if (myMatch.Success)
            {
                // Add your code here
            }发布于 2011-05-05 19:37:25
您的正则表达式只匹配小写字母,但是您的示例使用大写字母。
如果只想匹配大写字母,请使用以下命令:
[A-Z][0-9][A-Z][0-9][A-Z][0-9]如果要匹配大小写字母,请使用以下命令:
[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9]https://stackoverflow.com/questions/5896967
复制相似问题