簡體   English   中英

Java使用正則表達式匹配測驗模式

[英]Java using regex to match a pattern for quizzes

我正在嘗試做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}”)。

我做錯了什么嗎? 我遇到的另一個問題是,這只是返回一場比賽,而不是所有比賽。 你究竟如何遍歷字符串以查找所有匹配項? 任何幫助,將不勝感激。

我個人不會使用正則表達式,我只會在\\ n上使用StringTokenizer,並檢查第一個字符是否為數字(因為沒有其他行似乎以數字開頭)。

但更具體地回答你的問題。 您需要在模式上為^和$指定MULTILINE標志以匹配行的開頭和結尾。

Pattern pattern = Pattern.compile("^[0-9]{1}.+\\?", Pattern.MULTILINE);

這應該允許您的模式匹配文本中的行。 否則^和$只匹配字符串的開頭和結尾。

描述

此表達式將捕獲整個問題,然后是所有可能的答案,前提是字符串的大致格式與示例文本類似

^\\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

如果你使用String.matches() ,你只需要一小部分你正在嘗試使用的代碼。

要測試一行是否是一個問題:

if (line.matches("\\s*\\d\\..*"))

要測試一條線是否是答案:

if (line.matches("\\s*[A-Z]\\..*"))
  1. 在代碼中,quizLine類似於“1.以下哪一個訪問結構b中的變量?\\ nA.b-> var \\ nB.b.var \\ n ...”。 模式“^ [0-9] {1}。+ \\?” 將嘗試匹配整個字符串,這是不正確的。
  2. 這樣做的簡單方法是quizLine.split,並逐行匹配
  3. 另一種方法是@Denomales和@Chase描述,使用多行匹配,並獲得匹配組。
  4. 正如@Bohemian所說,String#matches是檢查字符串是否匹配但是無法獲得匹配組的好快捷方式。 如果你需要Matcher,請注意Matcher #lookingAt與Matcher#matches有點不同。 匹配#匹配可能會更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM