2012-12-22 203 views
1

我想通過使用正則表達式來驗證C#文本框中的輸入。預期的輸入是這種格式: CCCCC-CCCCC-CCCCC-CCCCC-CCCCC-CCCCC-C與正則表達式匹配的正則表達式

所以我有六個元素中的五個分隔字符和一個分隔字符的結尾。 .{5,255}

如何做我需要對其進行修改,以符合上述格式:

現在我正則表達式五255個字符之間的任何字符相匹配?

+0

C可以是任何字母數字字符(A-Z,a-z和0-9) – SeToY

+0

@SeToY ..你想讓這些字符相同嗎?這是允許的 - 「ABCDS-ASDFS-23423 ...」? –

+0

@RohitJain這很好。 – SeToY

回答

3

更新: -

如果你想匹配任何字符,那麼你可以使用: -

^(?:[a-zA-Z0-9]{5}-){6}[a-zA-Z0-9]$ 

說明: -

(?:    // Non-capturing group 
    [a-zA-Z0-9]{5} // Match any character or digit of length 5 
    -    // Followed by a `-` 
){6}    // Match the pattern 6 times (ABCD4-) -> 6 times 
[a-zA-Z0-9]  // At the end match any character or digit. 

注: -像您發佈的下面的正則表達式將只匹配模式: -

CCCCC-CCCCC-CCCCC-CCCCC-CCCCC-CCCCC-C 

你可以試試這個正則表達式: -

^(?:([a-zA-Z0-9])\1{4}-){6}\1$ 

說明: -

(?:    // Non-capturing group 
    (    // First capture group 
    [a-zA-Z0-9] // Match any character or digit, and capture in group 1 
) 
    \1{4}   // Match the same character as in group 1 - 4 times 
    -    // Followed by a `-` 
){6}    // Match the pattern 6 times (CCCCC-) -> 6 times 
\1     // At the end match a single character. 
+0

注意解釋不再相關。 –

+0

您可以使用http://www.regex101.com/來爲您的正則表達式生成準確的解釋:) –

+0

@Lindrian。不是我的準確?無論如何感謝鏈接。 –

1

未經檢驗的,但我認爲這將工作:

([A-Za-z0-9]{5}-){6}[A-Za-z0-9] 
+0

這導致有五個破折號'1 ----- 2 ----- 3 ----- 4 ----- 5 ----- 6 -----' – SeToY

+0

Ooops。小姐放置了{5}。修正了,謝謝。 – Michael

+0

現在這會匹配 - 'ABCDS-ASDFS-SFSAF-ASFSD -....' –

1

對於示例,一般更換C的字符類,你想:

^(C{5}-){6}C$ 

^([a-z]{5}-){6}[a-z]$  # Just letter, use case insensitive modifier 

^([a-z0-9]{5}-){6}[a-z0-9]$ # Letters and digits.. 
0

試試這個:

^(C{5}-){6}C$ 

^$表示字符串的begiining和結束repectively,使確保沒有輸入其他字符。