Roedy Green said:
Regexes are black box. If they fail, you have no idea where.
It would be nice if Matcher had some debugging methods to tell you the
offset and length of the best/longest match it was able to make, even
if it did not completely match.
If your regex is so complicated that you can't find the error after a
few minutes search with the Mark I eyeball, you should break it up
into multiple passes or rewrite your parsing code to not use a regex.
For long regexes, it can be a good idea to format it across multiple
lines with comments:
Pattern exampleRegex =
Pattern.compile( "^\\s*" // Start of string, optional whitespace
+ "(19[5-9][0-9]|20[0-9][0-9])" // G1: four digit year, from 1950 to 2099.
+ "-(0[1-9]|1[012])" // '-' divisor, G2: month, 01 to 12
+ "

(\\w+\\s?)*)" // ':' divisor, G3: one or more words, separated by a
// single whitespace
+ "

(.+)?$" // Optional: ':' divisor, G6: Remaining contents of line
);