2017-05-26 64 views
-1

我有正則表達式的字符串,其格式應該像如何正則表達式,長度爲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

回答

1

此正則表達式可表示如下

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

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

+0

@Polyfun所以我做了,很好發現 – Jamiec

0

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

使用

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)。 –

相關問題