如何正则表达式,长度为15的字符串为特定图案

问题描述:

我有正则表达式的字符串,其格式应该像如何正则表达式,长度为15的字符串为特定图案

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#。通过字符来检查条件。但这看起来不是理想的解决方案。 请协助使用正则表达式/ C#

+0

请参阅https://www.dotnetperls.com/regex – ThrowingSpoon

+4

尝试使用“@”^ [0-9] {2} [a-zA-Z] {5} [0-9] {4} [ a-za-z] [a-zA-Z0-9] {3} $“' –

+0

快速尝试一下你的正则表达式:http://regexr.com/ – ThrowingSpoon

此正则表达式可表示如下

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

您使用\ d和0-9作为数字;可能最好保持一致。 – Polyfun

+0

@Polyfun所以我做了,很好发现 – Jamiec

我想你需要匹配所定义的模式整个字符串相匹配。

使用

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"); 

如果你需要使它支持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)字符跟随它,并且是字符串中的最后一个字符,则匹配将失败。
+0

另外,看[RegexStorm上的正则表达式](http://regexstorm.net/tester?p=%5cA%5b0-9%5d%7b2%7d%5ba-zA- ž%5D%7B5%7D%5b0-9%5D%7B4%7D%5BA-ZA-Z%5D%5BA-ZA-Z0-9%5D%7B3%7D%5cz&I = 12abcde3456f0a1)。 –