Java使用正则表达式匹配测验模式
我正在尝试做100个大型项目之一。其中一个是关于一个小测验制作者,他分析一个小测验问题文件,随机挑选其中一些,创建一个小测验,并对小测验打分 我试图做的部分只是加载测验问题并单独解析它们(即1个问题及其作为一个实体的多项选择答案) 问答比赛的形式如下:Java使用正则表达式匹配测验模式,java,regex,string,Java,Regex,String,我正在尝试做100个大型项目之一。其中一个是关于一个小测验制作者,他分析一个小测验问题文件,随机挑选其中一些,创建一个小测验,并对小测验打分 我试图做的部分只是加载测验问题并单独解析它们(即1个问题及其作为一个实体的多项选择答案) 问答比赛的形式如下: Intro to Computer Science 1. Which of the following accesses a variable in structure b? A. b->var B. b.var
Intro to Computer Science
1. Which of the following accesses a variable in structure b?
A. b->var
B. b.var
C. b-var
D. b>var
2. Which of the following accesses a variable in a pointer to a structure, *b?
A. b->var
B. b.var
C. b-var
D. b>var
3. Which of the following is a properly defined struct?
A. struct {int a;}
B. struct a_struct {int a;}
C. struct a_struct int a
D. struct a_struct {int a;}
4. Which properly declares a variable of struct foo?
A. struct foo
B. foo var
C. foo
D. int foo
当然有很多这样的问题,但它们都是相同的格式。现在我使用BufferedReader将这些问题加载到字符串中,并尝试使用正则表达式来解析它们。但我无法在任何特定部分进行匹配。下面是我的代码:
package myPackage;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QuizMaker {
public static void main(String args[])
{
String file = "myfile/QuizQuestions.txt";
StringBuilder quizLine = new StringBuilder();
String line = null;
try {
FileReader reader = new FileReader(file);
BufferedReader buffreader = new BufferedReader(reader);
while ((line = buffreader.readLine()) != null)
{
quizLine.append(line);
quizLine.append("\n");
}
buffreader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
System.out.println(quizLine.toString());
Pattern pattern = Pattern.compile("^[0-9]{1}.+\\?");
Matcher matcher = pattern.matcher(quizLine.toString());
boolean didmatch = matcher.lookingAt();
System.out.println(didmatch);
String mystring = quizLine.toString();
int start = matcher.start();
int end = matcher.end();
System.out.println(start + " " + end);
char a = mystring.charAt(0);
char b = mystring.charAt(6);
System.out.println(a + " " + b);
}
}
在这一点上,我只是试图匹配问题本身,并留下多项选择答案,直到我解决这一部分。是因为我的正则表达式模式错误吗?我试图对一个简单的数字本身进行偶数匹配,但即使这样也失败了(通过“^[0-9]{1}”)
我做错什么了吗?我还有一个问题,那就是这只是返回了一场比赛,而不是全部比赛。您将如何遍历字符串以查找所有匹配项?任何帮助都将不胜感激。我个人不会使用正则表达式,我只会在\n上使用StringTokenizer,并检查第一个字符是否是数字(因为没有其他行以数字开头) 但更具体地回答你的问题。您需要在图案上为“^”和“$”指定多行标志,以匹配行的开始和结束
Pattern pattern = Pattern.compile("^[0-9]{1}.+\\?", Pattern.MULTILINE);
这将允许您的模式匹配文本中的线条。否则,^和$只需将字符串的开头和结尾作为一个整体进行匹配。Description
此表达式将捕获整个问题,然后是所有可能的答案,前提是字符串的格式与示例文本大致相同
^\s*(\d+\.\s+.*)(=[\r\n]+^\s*\d+\.\124;\ Z)
例子
实例:
给定示例文本
Intro to Computer Science
1. Which of the following accesses a variable in structure b?
A. b->var
B. b.var
C. b-var
D. b>var
2. Which of the following accesses a variable in a pointer to a structure, *b?
A. b->var
B. b.var
C. b-var
D. b>var
3. Which of the following is a properly defined struct?
A. struct {int a;}
B. struct a_struct {int a;}
C. struct a_struct int a
D. struct a_struct {int a;}
4. Which properly declares a variable of struct foo?
A. struct foo
B. foo var
C. foo
D. int foo
捕获第1组匹配项
[0] => 1. Which of the following accesses a variable in structure b?
A. b->var
B. b.var
C. b-var
D. b>var
[1] => 2. Which of the following accesses a variable in a pointer to a structure, *b?
A. b->var
B. b.var
C. b-var
D. b>var
[2] => 3. Which of the following is a properly defined struct?
A. struct {int a;}
B. struct a_struct {int a;}
C. struct a_struct int a
D. struct a_struct {int a;}
[3] => 4. Which properly declares a variable of struct foo?
A. struct foo
B. foo var
C. foo
D. int foo
如果您使用的是
String.matches()
,那么您只需要尝试使用的代码的一小部分
要测试行是否为问题,请执行以下操作:
if (line.matches("\\s*\\d\\..*"))
要测试一行是否为答案,请执行以下操作:
if (line.matches("\\s*[A-Z]\\..*"))
您需要重新考虑您的应用程序设计和数据的存储方式。考虑一个数据库(轻量级SOLN的SQLite)或XML文件或在当前方法中的序列化,当您保存测试分数时,很快就会变得一团糟。只有一个问题,一个选项“E.以上所有”都足以打破你当前的逐行解析的方法。是的,我确实想改进这一部分,但首先我想把基础弄下来并开始优化它。就目前而言,我并没有太多我认为这可能更好的东西。感谢这项工作,但正如您所说,使用stringtokenizer会更好,所以我会选择它而不是regex。