通过正则表达式采摘范围百分比
Plucking ranged percentages via regex
我正在接受用户输入的字符串并尝试确定它是否与正则表达式匹配。如果是这样,我需要从输入中提取某些匹配的组并处理它们。
有效的字符串必须是以下形式:
- 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
}
无效。谁能发现我哪里出错了?
我修改了您的模式以满足您的要求,如下所示。
([0-9]{1,2}(\.[0-9]{1,2})*)-([0-9]{1,2}(\.[0-9]{1,2})*)[%]*
我只在小数点和第二个数字部分添加了可选的(*
)。
而且我假设小数点前后的数字限制为1到2的长度,虽然你没有这么说
使用
Pattern pattern = Pattern.compile("(\d+(?:\.\d+)?)-(\d+(?:\.\d+)?)%*");
参见regex proof。
解释
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
\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
--------------------------------------------------------------------------------
- '-'
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
\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
--------------------------------------------------------------------------------
%* '%' (0 or more times (matching the most
amount possible))
我正在接受用户输入的字符串并尝试确定它是否与正则表达式匹配。如果是这样,我需要从输入中提取某些匹配的组并处理它们。
有效的字符串必须是以下形式:
- 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
}
无效。谁能发现我哪里出错了?
我修改了您的模式以满足您的要求,如下所示。
([0-9]{1,2}(\.[0-9]{1,2})*)-([0-9]{1,2}(\.[0-9]{1,2})*)[%]*
我只在小数点和第二个数字部分添加了可选的(*
)。
而且我假设小数点前后的数字限制为1到2的长度,虽然你没有这么说
使用
Pattern pattern = Pattern.compile("(\d+(?:\.\d+)?)-(\d+(?:\.\d+)?)%*");
参见regex proof。
解释
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
\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
--------------------------------------------------------------------------------
- '-'
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
\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
--------------------------------------------------------------------------------
%* '%' (0 or more times (matching the most
amount possible))