嘿大家, 我在设置正则表达式时遇到了一些小难题,该正则表达式将用户在文本框中输入的句子评估为关键字。实质上,关键字必须从一个连续输入到另一个,并且可以在之前,之间和之后具有任意数量的字符或空格(即,如果关键字是“乌鸦”和“英尺”,则乌鸦必须位于因此考虑到这一点,这个陈述应该是有效的“)。在一定程度上,字符和空间(我希望关键字在开始和结尾至少有一个空格缓冲区)是完全可选的,主要关心的是关键字是否按照正确的顺序输入。用于验证问题答案的正则表达式
到目前为止,我能够在一个句子中使用正则表达式工作,但如果仅输入答案本身,则无法工作。
我在下面的函数中使用正则表达式:
// 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;
任何帮助将大大赞赏。 谢谢。
关键词“鱼尾纹”仅仅是一个例子,它可以是任何数量的不同的词。 – Steve 2011-03-16 23:37:28