在Flutter中用于匹配3位小数的正则表达式:

huangapple go评论69阅读模式
英文:

Regular expression for 3 decimal point in flutter

问题

我想要实现以下功能:

小数点前有3位数字,小数点后只有1位数字
(同时还要检查前3位数字不能以0开头)

  • 正确:230.8,179.0,200.0等
  • 不正确:012.4,002.5,12.,12.22,1.4,1.222

目前我有以下代码:

TextFormField(
  focusNode: participantHeightFocus,
  // inputFormatters: [FilteringTextInputFormatter.digitsOnly],
  inputFormatters: [
    FilteringTextInputFormatter.allow(
        RegExp(r'^\d+\.?\d*$')),
  ],
  validator: (value) {
    if (value!.isEmpty) {
      return "Height is required";
    } else if (!RegExp(r'^\d+(\.\d+)').hasMatch(value)) {
      return "Please enter the height in cm (e.g., 170.0 cm)";
    }
    return null;
  },
  controller: participantHeight,
  onChanged: (_) => calculateBMIAndUpdate(),

  decoration: InputDecoration(
    hintText: "Participant Height (*)",
    labelText: "Participant Height (*)",
    labelStyle: const TextStyle(fontSize: 20),
    border: OutlineInputBorder(
        borderRadius: BorderRadius.circular(5)),
  ),
),

它会显示错误,但允许我在小数点前输入多个数字,小数点后输入多个数字,还允许我以0开头,这是错误的。

英文:

I want to achieve the following:

3 digits before decimal point and a single digit after it
( while it also checks that starting 3 digits that are added should not have 0 as the first digit )

  • Correct: 230.8, 179.0, 200.0 etc
  • Incorrect: 012.4, 002.5, 12. , 12.22, 1.4, 1.222

for now i have this

  TextFormField(
    focusNode: participantHeightFocus,
    // inputFormatters: [FilteringTextInputFormatter.digitsOnly],
    inputFormatters: [
      FilteringTextInputFormatter.allow(
          RegExp(r'^\d+\.?\d*$')),
    ],
    validator: (value) {
      if (value!.isEmpty) {
        return "Height is required";
      } else if (!RegExp(r'^\d+(\.\d+)').hasMatch(value)) {
        return "Please enter the height in cm (e.g., 170.0 cm)";
      }
      return null;
    },
    controller: participantHeight,
    onChanged: (_) => calculateBMIAndUpdate(),

    decoration: InputDecoration(
      hintText: "Participant Height (*)",
      labelText: "Participant Height (*)",
      labelStyle: const TextStyle(fontSize: 20),
      border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(5)),
    ),
  ),

it displays error but lets me enter many digits before decimal and many digits after decimal and it also allows me to start my number with 0 which is wrong

答案1

得分: 0

r'^[1-9]\d{2}.\d$'
如果要使小数点和数字可选(例如,170.0也可以写成170),稍微调整一下:

r'^[1-9]\d{2}(.\d)?$'

解释:

  • ^ 匹配字符串的开头
  • [1-9] 匹配除了0以外的每个数字
  • \d{2} 匹配两个数字
  • .\d 匹配点和一个数字
  • $ 匹配字符串的结尾
  • (...)? 使括号内的表达式变成可选项
英文:

Try this one:

r'^[1-9]\d{2}\.\d$'

If you want the decimal point and digit to be optional (if e.g. 170.0 can also be written as 170), adjust it a little:

r'^[1-9]\d{2}(\.\d)?$'

Explanation:

  • ^ matches the start of the string
  • [1-9] matches every digit except 0
  • \d{2} matches two digits
  • \.\d matches the point and one digit
  • $ matches the end of the string
  • (...)? makes the expression inside the parantheses optional

huangapple
  • 本文由 发表于 2023年7月20日 14:27:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定