【问题标题】:Plucking ranged percentages via regex通过正则表达式提取范围百分比
【发布时间】:2022-01-07 00:21:25
【问题描述】:

我正在接受来自用户输入的字符串并尝试确定它是否与正则表达式匹配。如果是这样,我需要从输入中提取某些匹配的组并进行处理。

有效的字符串必须是以下形式:

  • 1 个数字,可以由整数或浮点数(小数点)组成
  • 后跟连字符 ("-")
  • 后跟另一个数字(与第一个相同)
  • 可选以百分号结尾(“%”)

有效字符串输入示例:

  • “6-9”
  • “6.5-9”
  • “6-9.24”
  • “6-9%”
  • “6.5-9%”
  • “6.24-9.23%”

我的最佳尝试:

String numRange = getFromUser();
Pattern pattern = Pattern.compile("([0-9]{1,2}\.[0-9]{1,2})-([0-9]{1,2}\.[0-9]{1,2})[%]*");
Matcher matcher = pattern.matcher(numRange);
if (matcher.matches()) {
    String lowRangeVal = matcher.group(1);
    String highRangeVal = matcher.group(2);

    // continue processing here
}

不起作用。谁能发现我哪里出错了?

【问题讨论】:

    标签: java regex java-8


    【解决方案1】:

    我修改了您的模式以满足您的要求,如下所示。

    ([0-9]{1,2}(\.[0-9]{1,2})*)-([0-9]{1,2}(\.[0-9]{1,2})*)[%]*
    

    我只在小数位和第二个数字部分添加了可选 (*)。 我假设小数点前后的数字限制在 1 到 2 之间,虽然你没有这么说。

    https://regex101.com/r/5fpUB2/1

    【讨论】:

    • 可选部分使用?定义。您的* 表示“任意数量的出现”,换句话说,允许出现多个。
    • @Holger 你说得对。 ?(零或一次)在这种情况下更合理。
    【解决方案2】:

    使用

    Pattern pattern = Pattern.compile("(\\d+(?:\\.\\d+)?)-(\\d+(?:\\.\\d+)?)%*");
    

    regex proof

    解释

    --------------------------------------------------------------------------------
      (                        group and capture to \1:
    --------------------------------------------------------------------------------
        \d+                      digits (0-9) (1 or more times (matching
                                 the most amount possible))
    --------------------------------------------------------------------------------
        (?:                      group, but do not capture (optional
                                 (matching the most amount possible)):
    --------------------------------------------------------------------------------
          \.                       '.'
    --------------------------------------------------------------------------------
          \d+                      digits (0-9) (1 or more times
                                   (matching the most amount possible))
    --------------------------------------------------------------------------------
        )?                       end of grouping
    --------------------------------------------------------------------------------
      )                        end of \1
    --------------------------------------------------------------------------------
      -                        '-'
    --------------------------------------------------------------------------------
      (                        group and capture to \2:
    --------------------------------------------------------------------------------
        \d+                      digits (0-9) (1 or more times (matching
                                 the most amount possible))
    --------------------------------------------------------------------------------
        (?:                      group, but do not capture (optional
                                 (matching the most amount possible)):
    --------------------------------------------------------------------------------
          \.                       '.'
    --------------------------------------------------------------------------------
          \d+                      digits (0-9) (1 or more times
                                   (matching the most amount possible))
    --------------------------------------------------------------------------------
        )?                       end of grouping
    --------------------------------------------------------------------------------
      )                        end of \2
    --------------------------------------------------------------------------------
      %*                       '%' (0 or more times (matching the most
                               amount possible))
    

    【讨论】:

      猜你喜欢
      • 2012-12-08
      • 1970-01-01
      • 2017-12-10
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      相关资源
      最近更新 更多