0

まず、私は正規表現を使用したN00bです。これは、ATMで苦労していて、方向性を模索している新しい言語のようなものです。

シリアル番号フィールドを持つフォームがありますが、フィールドの内容は3つの可能なルールのいずれかになります。

Rule1:長さは常に13です。位置1は常にCです。位置2-5は数値です。位置6はアルファです。位置7-13は数値です。

Rule2:長さは8または9です。位置1-2はアルファです。位置3-8は数値です。位置9はアルファです。

Rule3: 長さは9です位置1-9は数値です

私の質問:これは正規表現内で達成可能な式です–そしてサブ質問:私が見ることができる場所へのポインターまたは私が解読できる表現?

これがn00b'esqueすぎる場合は、お詫びします–RegExが私の頭を動かします:)

4

1 に答える 1

2

試してみてください:

/^(C\d{4}[a-zA-Z]\d{7}|[a-zA-Z]{2}\d{6}[a-zA-Z]?|\d{9})$/

ルール#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
----------------------------------------------------------------------
于 2013-01-19T10:44:11.660 に答える