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
-2位数字[0-9]{2}
-5个字母[a-zA-Z]{5}
-4位数字[0-9]{4}
-一封信[a-zA-Z]
-3个字母数字符号(支持Unicode的-[a-zA-Z0-9]{3}
)[\p{L}\p{N}]
-字符串的末尾。如果其后有换行符(LF)字符,并且是字符串中的最后一个字符,则匹配将失败\z
@“^[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");