1

最大長が 6 で、ドットの後に最大 2 つのシンボルを持つ浮動小数点数の正規表現を作成したいと考えています。

この値のいずれかを許可したい

XXXXXXX
XXXXX.X
XXXX.XX
XXX.XX
XX.XX
X.XX

私はこのようなことを試みています\d{1,4}\.?\d{0,2}が、この場合は入力できませんXXXXX.X

多分私は条件を使用する必要があります

4

2 に答える 2

3

私は内のケースXXXX.XXがあなたの正規表現によって処理されると信じています。したがって、他の 2 つのケースを個別に照合してみませんか (これに正規表現を使用することに熱心な場合)。何かのようなもの :

\d{1,4}\.?\d{0,2} | \d{5}\.?\d |\d{6}

于 2013-11-07T08:11:10.683 に答える
1

先読みを使用するのはどうですか:

^(?=\d{1,7}(?:\.\d{0,2})?).{1,7}$

説明:

The regular expression:

(?-imsx:^(?=\d{1,7}(?:\.\d{0,2})?).{1,7}$)

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
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    \d{1,7}                  digits (0-9) (between 1 and 7 times
                             (matching the most amount possible))
----------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
----------------------------------------------------------------------
      \.                       '.'
----------------------------------------------------------------------
      \d{0,2}                  digits (0-9) (between 0 and 2 times
                               (matching the most amount possible))
----------------------------------------------------------------------
    )?                       end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  .{1,7}                   any character except \n (between 1 and 7
                           times (matching the most amount possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2013-11-07T08:30:05.757 に答える