Note that there are some explanatory texts on larger screens.

plurals
  1. POJava using regex to match a pattern for quizzes
    primarykey
    data
    text
    <p>I am trying to do one of the 100 mega list projects. One of them is about a quiz maker that parses through a file of quiz questions, picks some of them out at random, creates a quiz and also grades quizzes. </p> <p>I am trying to do the part of simply loading in the quiz questions and parsing them out individually (i.e. 1 question and its multiple choice answers as an entity).</p> <p>The format of the quiz is as follows:</p> <pre><code>Intro to Computer Science 1. Which of the following accesses a variable in structure b? A. b-&gt;var B. b.var C. b-var D. b&gt;var 2. Which of the following accesses a variable in a pointer to a structure, *b? A. b-&gt;var B. b.var C. b-var D. b&gt;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 </code></pre> <p>Of course there are many of these questions but they are all in the same format.Now I used BufferedReader to load in these questions into a string and am attempting to use regex to parse them. But I am unable to match on any specific part. Below is my code:</p> <pre><code> 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); } } </code></pre> <p>At this point, I am simply trying to match on the questions themselves and leave the multiple choice answers till I solve this part. Is it due to my regex pattern being wrong? I tried to even match on a simple number itself and even that was failing (via "^[0-9]{1}"). </p> <p>Am I doing something completely wrong? One other question I had was that this simply was returning one match, not all of them. How exactly would you iterate through the string to find all matches? Any help would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload