开发者

Regular expression for validating an answer to a question

开发者 https://www.devze.com 2023-02-18 10:17 出处:网络
Hey everyone, I\'m having a minor difficulty setting up a regular expression that evaluates a sentence entered by a user in a textbox to keyword(s). Essentially, 开发者_JAVA技巧the keywords have to be

Hey everyone, I'm having a minor difficulty setting up a regular expression that evaluates a sentence entered by a user in a textbox to keyword(s). Essentially, 开发者_JAVA技巧the keywords have to be entered consecutive from one to the other and can have any number of characters or spaces before, between, and after (ie. if the keywords are "crow" and "feet", crow must be somewhere in the sentence before feet. So with that in mind, this statement should be valid "blah blah sccui crow dsj feet "). The characters and to some extent, the spaces (i would like the keywords to have at least one space buffer in the beginning and end) are completely optional, the main concern is whether the keywords were entered in their proper order.

So far, I was able to have my regular expression work in a sentence but failed to work if the answer itself was entered only.

I have the regular expression used in the function below:

// Comparing an answer with the right solution
protected boolean checkAnswer(String a, String s) {
    boolean result = false;
    //Used to determine if the solution is more than one word
    String temp[] = s.split(" ");

    //If only one word or letter
    if(temp.length == 1)
    {
        if (s.length() == 1) {
        // check multiple choice questions
            if (a.equalsIgnoreCase(s)) result = true;
            else result = false;
            }
            else {
                // check short answer questions
                if ((a.toLowerCase()).matches(".*?\\s*?" + s.toLowerCase() + "\\s*?.*?")) result = true;
                else result = false;
            }
    }
    else
    {
        int count = temp.length;
        //Regular expression used to
        String regex=".*?\\s*?";

        for(int i = 0; i<count;i++)
            regex+=temp[i].toLowerCase()+"\\s*?.*?";

        //regex+=".*?";
        System.out.println(regex);
        if ((a.toLowerCase()).matches(regex)) result = true;
        else result = false;
    }

    return result;

Any help would greatly be appreciated. Thanks.


I would go about this in a different way. Instead of trying to use one regular expression, why not use something similar to:

String answer = ... // get the user's answer

if( answer.indexOf("crow") < answer.indexOf("feet") )  {
    // "correct" answer
}

You'll still need to tokenize the words in the correct answer, then check in a loop to see if the index of each word is less than the index of the following word.


I don't think you need to split the result on " ".

If I understand correctly, you should be able to do something like

String regex="^.*crow.*\\s+.*feet.*"

The problem with the above is that it will match "feetcrow feetcrow".

Maybe something like

String regex="^.*\\s+crow.*\\s+feet\\s+.*"

That will enforce that the word is there as opposed to just in a random block of characters.


Depending on the complexity Bill's answer might be the fastest solution. If you'd prefer a regular expression, I wouldn't look for any spaces, but word boundaries instead. That way you won't have to handle commas, dots, etc. as well:

String regex = "\\bcrow(?:\\b.*\\b)?feet\\b"

This should match "crow bla feet" as well as "crowfeet" and "crow, feet".

Having to match multiple words in a specific order you could just join them together using '(?:\b.*\b)?' without requiring any additional sorting or checking.


Following Bill answer, I'd try this:

String input = // get user input
String[] tokens = input.split(" ");
String key1 = "crow";
String key2 = "feet";   
String[] tokens = input.split(" ");
List<String> list = Arrays.asList(tokens);
return list.indexOf(key1) < list.indexOf(key2)
0

精彩评论

暂无评论...
验证码 换一张
取 消