I am using a regular expression to match whether or not a pattern matches, but I also want to know when it fails.
For example, say I have a pattern of “N{1,3}Y”. I match it against string “NNNNY”. I would like to know that it failed because there were too many Ns. Or if I match it against string “XNNY”, I would like to know that it failed because an invalid character “X” was in the string.
From looking at the Java regular expression package API (java.util.regex), additional information only seems to be available from the Matcher class when the match succeeds.
Is there a way to resolve this issue? Or is regular expression even an option in this scenario?
I guess you should use a parser, rather than simple regular expressions.
Regular Expressions are good providing matches for string, but not quite so in providing NON-matches, let alone explaining why a match failed.