Reputation: 3743
SO I have the follow input string
OPERATION: 12, 12.32, 54.3332
OPERATION can be any of MIN, MAX, SUM. The regex should accept strings that only start with any of those three words follow by colon and the followed by either integer or double.
I have been googling and fidling around for hours now before I decided to turn to you guys.
Thanks for your time!
EDIT:
So far I have ended with this regex
^[MAX:SUM:AVERAGE:MIN:(\\d+(\\.\\d+)?), ]+$
It matches "MAX: " as correct as well as "MAX:" and "MA: . It also matches the strings of the following format : "MAX: 12, 12.3323......"
Upvotes: 0
Views: 495
Reputation: 55609
You misunderstand the meaning of []
. These refer to single character alternatives.
So [MAX:SUM:AVERAGE:MIN:(\\d+(\\.\\d+)?), ]
is either M
or A
or X
or .... So something like MMMMM
should also match.
You may want something more like this:
^(MAX|SUM|AVERAGE|MIN): (\\d+(\\.\\d+)?(, (?=.)|$))+$
Explanation:
(MAX|SUM|AVERAGE|MIN)
is either MAX
, SUM
, AVERAGE
or MIN
.
": "
refers to the actual characters :
and space.
\\d+(\\.\\d+)?
is what you had before.
", "
refers to the actual characters ,
and space.
(?=.)
is look-ahead. Checking that the following characters (?=)
matches any single character (.
) (thus not end-of-string) so there isn't a ", "
required at the end.
So , (?=.)|$
is either ", "
not at the end or nothing at the end.
Alternative: Not using look-ahead
^(MAX|SUM|AVERAGE|MIN): (\\d+(\\.\\d+)?(, \\d+(\\.\\d+)?)*)$
Upvotes: 2
Reputation: 20843
(?:MIN|MAX|SUM):[ ]+\d+(?:[.]\d+)?(?:,[ ]+\d+(?:[.]\d+)?)*
Explanation
(?:MIN|MAX|SUM) - either one of the operations, non-capturing
: - a literal :
[ ]+ - 1 or more space characters
\d+ - 1 or more digits
(?:[.]\d+)? - optionally a literal . followed by 1 or more digits, non-capturing
(?:,...)* - a literal comma and another number, 0 or more times, non-capturing
[.]
could also be written as \.
but putting magic characters in a character class instead is a best practice, same goes for using [ ]
instead of a space.
References
Hope that helps.
Upvotes: 2
Reputation: 179
java.util.regex.Pattern
accept regular expression formed by group of Greedy format expressions. You can create group to divide your input string.
OPERATION: NUM, NUM, DECIMAL, DECIMAL
Your regular expression is:
([MIN,MAX,SUM]+):([0-9]{4}.[0-9]{2}),(...),(...),(...)
and use:
Matcher.group()
to iterate upon found groups.
Note: I supposed that your number format is "dddd.dd"
Upvotes: 1