首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >复杂Regex -有可能吗?

复杂Regex -有可能吗?
EN

Stack Overflow用户
提问于 2013-01-19 10:37:13
回答 1查看 96关注 0票数 0

首先,我是一个使用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使我的头:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-19 10:44:11

试一试:

代码语言:javascript
运行
复制
/^(C\d{4}[a-zA-Z]\d{7}|[a-zA-Z]{2}\d{6}[a-zA-Z]?|\d{9})$/

我认为最后一个alpha在规则#2中是可选的。

解释:

代码语言:javascript
运行
复制
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
----------------------------------------------------------------------
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14413429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档