我对 RegEx 有疑问。如何获得至少包含 1 个数字和 1 个字母的字符串的 12 符号长部分?
示例:“这是 12 个符号长:F8ENL83I0E12,也许还有一些文字”
最佳答案
要在较长的文本中查找长度为 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
关于正则表达式字符串,12 个符号(至少 1 个数字和至少 1 个字母),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13564708/