我正在尝试做100个巨型项目中的一个。其中之一是关于一个测验制作程序,它解析一个测验问题文件,随机挑选其中的一些,创建一个测验,并对测验评分。
我试图做的是简单地加载测验问题并逐个解析出来(即一个问题及其多项选择答案作为一个实体)。
测验的形式如下:
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
当然,这些问题有很多,但它们都在相同的format.Now中,我使用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}")。
我是不是做错了什么?我遇到的另一个问题是,这只是返回一个匹配,而不是所有匹配。您将如何遍历字符串以查找所有匹配项?任何帮助都将不胜感激。
发布于 2013-07-12 03:04:03
如果你使用的是String.matches()
,你只需要一小部分你正在尝试使用的代码。
要测试某行是否为问题,请执行以下操作:
if (line.matches("\\s*\\d\\..*"))
要测试一行是否为答案,请执行以下操作:
if (line.matches("\\s*[A-Z]\\..*"))
发布于 2013-07-12 02:54:59
我个人不会使用正则表达式,我只会在\n上使用StringTokenizer,并且只检查第一个字符是否是数字(因为似乎没有其他行是以数字开头的)。
而是更具体地回答你的问题。您需要在您的模式中为^和$指定多行标志,以匹配行首和行尾。
Pattern pattern = Pattern.compile("^[0-9]{1}.+\\?", Pattern.MULTILINE);
这应该允许您的模式匹配文本中的行。否则^和$只是将字符串的开头和结尾作为一个整体进行匹配。
发布于 2013-07-12 03:02:05
描述
该表达式将捕获整个问题,然后是所有可能的答案,前提是字符串的格式与示例文本大致相同
^\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
https://stackoverflow.com/questions/17606624
复制