2

最大 7 桁の float に一致する正規表現を探しています。これで小数点を処理する方法がわかりません。これを正規表現と一致させることさえ可能ですか? 小数点の左側に少なくとも 1 桁、右側に 0 ~ 6 桁が必要ですが、合計桁数は 7 以下である必要があります。

例:

良い:

  • 1.234567
  • 0.1
  • 1234567
  • 1

悪い:

  • .1234567
  • 12345678
  • 1.2.34567
4

1 に答える 1

7

以下が機能するはずです。

^(?!.*\..*\.|\d{8})\d[\d.]{,7}$

例:http ://www.rubular.com/r/gglVngm0pH

説明:

^            # beginning of string anchor
(?!          # start negative lookahead (fail if following regex can match)
   .*\..*\.    # two or more '.' characters exist in the string
   |           # OR
   \d{8}       # eight consecutive digits in the string
)            # end negative lookahead
\d           # match a digit
[\d.]{,7}    # match between 0 and 7 characters that are either '.' or a digit
$            # end of string anchor
于 2013-02-19T17:50:00.647 に答える