2012-11-26 170 views
3

我在使用RegEx時遇到了問題。如何獲得至少包含1個數字和1個字母的字符串的12個符號長度部分?RegEx字符串,12個符號(至少1個數字和至少1個字母)

舉例:「這是12個標誌長:F8ENL83I0E12,也許一些文字」

+0

它是否只包含大寫字母和數字? – siebz0r

+0

不,小寫字母也是可以的。 – SoBiT

+0

看起來像[這](http://stackoverflow.com/questions/1050633/looking-for-a-regex-8-char-min-w-1-num-and-1-char)是你尋找。 – siebz0r

回答

1

要找到一個較長的文本內的長度爲12的字母詞,使用

(?i)    # Case-insensitive matching 
\b    # Start of word 
(?=[A-Z]*[0-9]) # Assert presence of at least one ASCII digit 
(?=[0-9]*[A-Z]) # Assert presence of at least one ASCII letter 
[A-Z0-9]{12}  # Match exactly 12 ASCII letters/digits 
\b    # End of word 

或(對於JavaScript,因爲它不支持詳細的正則表達式)

/\b(?=[A-Z]*[0-9])(?=[0-9]*[A-Z])[A-Z0-9]{12}\b/i 
+0

謝謝,很好解釋。 – SoBiT

相關問題