1

I want to know the exact position of an error when a regular expression match has failed. But I couldn't find any class or method in Matcher neither in Pattern that can do that. Any help?

Pattern regExpPattern = Pattern.compile("My regular expression");
Matcher matcher = regExpPattern.matcher("Input string");
if (!matcher.matches()) {
    // Better error handling here
    throw new Exception("Invalid expression");
}

EDIT: Here is a working code excerpt:

public class FOPRequestParser {

private static Pattern regExpPattern;
private static String requestRegexp;
private static String regexpSeparators;

private static String[] INPUT_FORMATS = new String[] {"FO", "IFP"};
private static String[] OUTPUT_FORMATS = new String[] {"PDF", "PS", "AFP", "IFP"};

static {
    regexpSeparators = new String("(\\Q|@|\\E|=)");

    requestRegexp = new String("run" + regexpSeparators + "[a-zA-Z]+.*"
            + regexpSeparators + "inputFormat=(");
    for (int i = 0; i < INPUT_FORMATS.length; ++i) {
        requestRegexp += INPUT_FORMATS[i];
        if (i != INPUT_FORMATS.length - 1) {
            requestRegexp += "|";
        }
    }
    requestRegexp += ")" + regexpSeparators + "[a-zA-Z]+.*"
            + regexpSeparators + "outputFormat=(";
    for (int i = 0; i < OUTPUT_FORMATS.length; ++i) {
        requestRegexp += OUTPUT_FORMATS[i];
        if (i != OUTPUT_FORMATS.length - 1) {
            requestRegexp += "|";
        }
    }
    requestRegexp += ")";
}


public FOPRequestParser() {
    regExpPattern = Pattern.compile(requestRegexp);
}

public void processRequest(String request) throws Exception {
    Matcher matcher = regExpPattern.matcher(request);
    if (!matcher.matches()) {
        throw new Exception("Invalid expression");
    }
}

public static void main(String[] args) {

    FOPRequestParser conversion = new FOPRequestParser();
    try {
        String exp1 = new String("run|@|" +
                "c:\\input_file.fo|@|" +
                "inputFormat=FO|@|" +
                "c:\\output_file.pdf|@|" +
                "outputFormat=PDF");
        conversion.processRequest(exp1);
        System.out.println("Valid expression");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

10
  • 3
    Could you give an example of input to the string and regex that would produce a "match error" along with the value you would like returned? Commented Dec 26, 2013 at 14:14
  • FWIW, you can use regex101 to debug your regex, though you'll have to convert them to PCRE format first (removing double escapes, and some classes Java can handle but not PCRE). Just put your string to match and regex, then click on the little bug icon on the left. Commented Dec 26, 2013 at 14:15
  • My regular expression is fine. It works as it should! The issue is that I want to know more informations in case of match errors, like for instance the index in the input string where a "bad" character was found. I'll post the full code along with a simple example. Commented Dec 26, 2013 at 14:18
  • 3
    There is no error when no match is found, there simply was no match. Commented Dec 26, 2013 at 14:18
  • 1
    When something doesn't match, it "doesn't match" everywhere. Matching doesn't stop at a certain position. Commented Dec 26, 2013 at 14:24

2 Answers 2

1

There are no API methods that tell you why there was not match.

If you use this for any sort of (input) validation you'd have to throw a generic exception saying something like "The provided value does not match the expected pattern <put-regex-or-human-readable-pattern-here>".

Sign up to request clarification or add additional context in comments.

Comments

1

What is with this solution. It is not really nice but for my needs it worked:

int index = -1;
try {
     Field declaredField = Matcher.class.getDeclaredField("last");      
     declaredField.setAccessible(true); 
     index =Integer.parseInt(declaredField.get(matcher).toString()); } catch
(Exception e) { }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.