首先,我是一个使用Regex的N00b --这就像一种新的语言,我正在与自动取款机做斗争,并在寻找方向。
我有一个有序列号字段的表单,但是字段内容可以是3条可能的规则之一。
Rule1 :长度总是13位置1总是C位置2-5是数字位置6是alpha位置7-13是数字 Rule2 :长度为8或9位置1-2为alpha位置3-8为数字位置9为alpha Rule3 :长度为9,位置1-9为数字
我的问题:公式在正则表达式中是可以实现的吗?子问题:指向哪里的指针,或者我能解释的表达式?
抱歉,如果这是n00b‘’esque RegEx使我的头:)
发布于 2013-01-19 10:44:11
试一试:
/^(C\d{4}[a-zA-Z]\d{7}|[a-zA-Z]{2}\d{6}[a-zA-Z]?|\d{9})$/
我认为最后一个alpha在规则#2中是可选的。
解释:
The regular expression:
(?-imsx:^(C\d{4}[a-zA-Z]\d{7}|[a-zA-Z]{2}\d{6}[a-zA-Z]?|\d{9})$)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
C 'C'
----------------------------------------------------------------------
\d{4} digits (0-9) (4 times)
----------------------------------------------------------------------
[a-zA-Z] any character of: 'a' to 'z', 'A' to 'Z'
----------------------------------------------------------------------
\d{7} digits (0-9) (7 times)
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[a-zA-Z]{2} any character of: 'a' to 'z', 'A' to 'Z'
(2 times)
----------------------------------------------------------------------
\d{6} digits (0-9) (6 times)
----------------------------------------------------------------------
[a-zA-Z]? any character of: 'a' to 'z', 'A' to 'Z'
(optional (matching the most amount
possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\d{9} digits (0-9) (9 times)
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
https://stackoverflow.com/questions/14413429
复制相似问题