I am writing a CSV parser and in order to detect the different data types I can expect to get on the files, I have a Map for each data type, each containing the regexes that I defined as valid and recognizable. For instance, for my Integer values, here is my Map:
Map<String, String> integerFormatRegexps = new HashMap<String, String>();
integerFormatRegexps.put("^[1-9]\\d{1,9}$", "##0");
integerFormatRegexps.put("^-[1-9]\\d{1,9}$", "-##0");
integerFormatRegexps.put("^0$", "0");
Now, I've seen several examples here in SO where instead of having these regexes separated, they use Alternations, where instead of three regex, I could use just one:
Map<String, String> integerFormatRegexps = new HashMap<String, String>();
integerFormatRegexps.put("^[1-9]\\d{1,9}$|^-[1-9]\\d{1,9}$|^0$", "Integer");
My questions is which of the two approaches would be more efficient in general, when matching patterns in Java? Iterating through the separate simpler regexes to find a match, or matching against just one, more complex regex?