I want to try to filter all operators and operands including floating points
from a commandline input with regexp. the operators work fine and the floating point works when i use it with a seperate matcher but i want to solve this in one regexp term
my code till now does
Matcher numbers = Pattern.compile("[0-9]*\\.?[0-9]+").matcher(expr);
Matcher m = Pattern.compile("[\\+|\\*|/|\\-|\\^|\\!|_|([0-9]*\\.?[0-9]+)]").matcher(expr);
while(numbers.find()) {
System.out.print(" " + numbers.group() + " num \n");
}
while (m.find()) {
System.out.print(" " + m.group() + " -- \n");
}
heres the output
3.0 num
2 num
3 —
. —
0 —
2 —
as you see the numbers matcher just finds floating points and normal numbers and that works fine. but if i use it in the second matcher devided by an or
i get 3 . 0 as separate matches.
IIRC Java regexes use parentheses for a grouping operator, not square brackets like you’ve got there. Have you tried
instead? Because what you’ve got is just a large, multiply redundant character set: It should merely match any one of the characters between the first left square bracket and the final right square bracket. That seems to be the behavior you’re seeing.
Or did I completely misunderstand the intent of the second regex?