So I have a String I want to split into tokens of different types as part of a larger Parser.
String input = "45 + 31.05 * 110 @ 54";
I use javas regex libraries Pattern and Matcher to interpret my regexes and find matches.
String floatRegex = "[0-9]+(\\.([0-9])+)?";
String additionRegex = "[+]";
String multiplicationRegex = "[*]";
String integerRegex = "[0-9]+"
All my regexes gets merged into a single master regex with pipe symbols between the different regexes.
String masterOfRegexes = "[0-9]+(\\.([0-9])+)?|[+]|[*]|[0-9]+"
I send this pattern into Pattern.compile() and get the matcher. As I step though from left to right running matcher.find(), I expect to get this structure out, up to the point of the "@" symbol where an InvalidInputException should be thrown.
[
["Integer": "45"],
["addition": "+"],
["Float": "31.05"],
["multiplication": "*"],
["Integer": "110"]
Exception should be thrown...
]
Problem is that matcher.find() skips the "@" symbol completely and instead find the match of the next Integer past "@", which is "54".
Why does it skip the "@" symbol and how can I make it so the exception gets thrown on a character it doesn't recognize from my pattern?
([0-9]+(?:\.[0-9]+)?|[+]|[*]|[0-9]+)|\S+and check for group 1. If group 1 is null, then you can throw your exception. See regex101.com/r/0RmvB1/1 and see ideone.com/hzWmuF