我正在尝试做100个大型项目中的一个。其中一个是关于一个测验制作者,它通过一个测验问题的档案进行解析,随机选择其中一些,创建一个测验,并对测验进行评分。
我正在尝试简单地加载测验问题并单独解析它们(即1个问题及其多选答案作为实体)。
测验的格式如下:
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}”)。
我做错了吗?我遇到的另一个问题是,这只是返回一场比赛,而不是所有比赛。你究竟如何遍历字符串以查找所有匹配项?任何帮助将不胜感激。
答案 0 :(得分:1)
我个人不会使用正则表达式,我只会在\ n上使用StringTokenizer,并检查第一个字符是否为数字(因为没有其他行似乎以数字开头)。
但更具体地回答你的问题。您需要在模式上为^和$指定MULTILINE标志以匹配行的开头和结尾。
Pattern pattern = Pattern.compile("^[0-9]{1}.+\\?", Pattern.MULTILINE);
这应该允许您的模式匹配文本中的行。否则^和$只匹配字符串的开头和结尾。
答案 1 :(得分:1)
此表达式将捕获整个问题,然后是所有可能的答案,前提是字符串的大致格式与示例文本类似
^\s*(\d+\.\s+.*?)(?=[\r\n]+^\s*\d+\.|\Z)
直播示例:http://www.rubular.com/r/dcetgPsz5w
给定示例文字
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
答案 2 :(得分:1)
如果您使用String.matches()
,则只需要当前尝试使用的代码的一小部分。
测试一行是否是一个问题:
if (line.matches("\\s*\\d\\..*"))
测试一条线是否是答案:
if (line.matches("\\s*[A-Z]\\..*"))
答案 3 :(得分:0)