I am accepting a string from user input and trying to determine if it matches a regex. If it does I need to pluck certain matched groups out of the input and process them.
The valid string must be of the form:
- 1 number that can consist of integers or floating points (decimal point)
- followed by a hyphen ("
-") - followed by another number (same as the first)
- optionally ended with a percent sign ("
%")
Examples of valid string inputs:
- "6-9"
- "6.5-9"
- "6-9.24"
- "6-9%"
- "6.5-9%"
- "6.24-9.23%"
My best attempt:
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
}
Does not work. Can anyone spot where I'm going awry?
CodePudding user response:
I modified your pattern to fit your requirement as following.
([0-9]{1,2}(\.[0-9]{1,2})*)-([0-9]{1,2}(\.[0-9]{1,2})*)[%]*
I only added optional (*) to the decimal place and second number part.
And I assumed that number before or after the decimal place is limited to length 1 to 2, although you did not say so.
https://regex101.com/r/5fpUB2/1
CodePudding user response:
Use
Pattern pattern = Pattern.compile("(\\d (?:\\.\\d )?)-(\\d (?:\\.\\d )?)%*");
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
( 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))
