Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在特定模式中正则化长度为15的字符串_C#_Regex - Fatal编程技术网

C# 如何在特定模式中正则化长度为15的字符串

C# 如何在特定模式中正则化长度为15的字符串,c#,regex,C#,Regex,我必须正则化一个字符串,它的格式应该是 Position Format 1st Numeric 2nd Numeric 3rd Alphabet 4th Alphabet 5th Alphabet 6th Alphabet 7th Alphabet 8th Numeric 9th Numeric 10th Numeric 11th Numeric 12th

我必须正则化一个字符串,它的格式应该是

Position  Format
1st       Numeric
2nd       Numeric
3rd       Alphabet
4th       Alphabet
5th       Alphabet
6th       Alphabet
7th       Alphabet
8th       Numeric
9th       Numeric
10th      Numeric
11th      Numeric
12th      Alphabet
13th      AlphaNumeric
14th      AlphaNumeric
15th      AlphaNumeric
然后最后必须匹配正则表达式是否有效

Match match = Regex.Match( inputString, regex, RegexOptions.IgnoreCase );

if ( inputString != string.Empty && match.Success )
{
     // Condition
}
我真的被卡住了。我正在使用c#。循环遍历字符并检查条件。但这看起来并不是一个理想的解决方案。
请协助使用Regex/C#

此Regex可以表示为

\d{2}[a-zA-Z]{5}\d{4}[a-zA-Z][\da-zA-Z]{3}

我假设您需要匹配与您定义的模式匹配的整个字符串

使用

如果需要识别Unicode,请将所有
[0-9]
替换为
\d
,将所有
[a-zA-Z]
替换为
\p{L}

详细信息

  • \A
    -字符串的开头
  • [0-9]{2}
    -2位数字
  • [a-zA-Z]{5}
    -5个字母
  • [0-9]{4}
    -4位数字
  • [a-zA-Z]
    -一封信
  • [a-zA-Z0-9]{3}
    -3个字母数字符号(支持Unicode的-
    [\p{L}\p{N}]
  • \z
    -字符串的末尾。如果其后有换行符(LF)字符,并且是字符串中的最后一个字符,则匹配将失败

请参见Try
@“^[0-9]{2}[a-zA-Z]{5}[0-9]{4}[a-zA-Z][a-zA-Z0-9]{3}$”
使用
\d{2}[a-zA-Z]{5}\d{4}[a-zA-Z][a-zA-Z-d]{3}快速试用您的正则表达式?它是否包含重音字符?您同时使用\d和0-9表示数字;“可能最好是保持一致性。”Polyfun是这样做的,也很好地指出了这一点,请参见。
var isValid = Regex.IsMatch(s, @"\A[0-9]{2}[a-zA-Z]{5}[0-9]{4}[a-zA-Z][a-zA-Z0-9]{3}\z");