首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用正则表达式提取范围百分比

用正则表达式提取范围百分比
EN

Stack Overflow用户
提问于 2022-01-07 00:21:25
回答 2查看 35关注 0票数 0

我接受来自用户输入的字符串,并试图确定它是否与regex匹配。如果是这样的话,我需要从输入中提取出某些匹配的组并对它们进行处理。

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

  • 1可以由整数或浮点数组成的数字(小数点)
  • 后面接连字符("-")
  • 后面跟着另一个数字(与第一个数字相同)
  • 可选地以百分比符号结束 ("%")

有效字符串输入的示例:

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

我最好的尝试:

代码语言:javascript
复制
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
}

不管用。有人能发现我哪里出问题了吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-07 00:31:05

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

代码语言:javascript
复制
([0-9]{1,2}(\.[0-9]{1,2})*)-([0-9]{1,2}(\.[0-9]{1,2})*)[%]*

我只在小数位和第二个数字部分中添加了可选的(*)。我假设小数位之前或之后的数字被限制在长度1到2,尽管你没有这么说。

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

票数 2
EN

Stack Overflow用户

发布于 2022-01-07 00:49:10

使用

代码语言:javascript
复制
Pattern pattern = Pattern.compile("(\\d+(?:\\.\\d+)?)-(\\d+(?:\\.\\d+)?)%*");

正则证明

解释

代码语言:javascript
复制
--------------------------------------------------------------------------------
  (                        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))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70615198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档