自定义Java正则表达式:匹配项以

时间:2018-07-18 14:15:14

标签: java regex apache-poi file-processing

我已经为此苦苦挣扎了几天,我想知道也许有人可以帮我解决这个问题。

我要完成的工作是处理一个包含一系列问题和答案的文本文件。文件内容(.doc或.docx)如下:

Document Name
1. Question one:
a. Answer one to question one
b. Answer two to question one
c. Answer three to question one
2. Question two:
a. Answer one to question two
c. Answer two to question two
e. Answer three to question two

到目前为止,我尝试过的是:

像这样通过Apache POI读取文档内容:

fis = new FileInputStream(new File(FilePath));
XWPFDocument doc = new XWPFDocument(fis);
XWPFWordExtractor extract = new XWPFWordExtractor(doc);
String extractorText = extract.getText();

所以,到现在为止,我已经有了文件的内容。接下来,我尝试创建一个正则表达式模式,该模式将与问题开头的数字和点( 1。 12。)匹配,并继续进行到它通过以下方式匹配冒号:

Pattern regexPattern = Pattern.compile("^(\\d|\\d\\d)+\\.[^:]+:\\s*$", Pattern.MULTILINE);
Matcher regexMatcher = regexPattern.matcher(extractorText);

但是,当我尝试遍历结果集时,找不到任何问题文本:

while (regexMatcher.find()) {
    System.out.println("Found");
    for (int i = 0; i < regexMatcher.groupCount() - 2; i += 2) {
        map.put(regexMatcher.group(i + 1), regexMatcher.group(i + 2));
        System.out.println("#" + regexMatcher.group(i + 1) + " >> " + regexMatcher.group(i + 2));
    }
}

由于我是Java的新手,因此我不确定我要去哪里出错,并希望有人可以帮助我。

此外,如果有人对如何用问题和与之相关的答案来创建地图有更好的方法,将不胜感激。

谢谢。

编辑:我正在尝试获取类似Map之类的东西,其中将包含键(问题文本)和另一个字符串列表,这些字符串代表与该问题相关的答案集,例如:

Map<String, List<String>> desiredResult = new HashMap<>();
    desiredResult.entrySet().forEach((entry) -> {
        String       questionText = entry.getKey();
        List<String> answersList  = entry.getValue();

        System.out.println("Now at question: " + questionText);

        answersList.forEach((answerText) -> {
            System.out.println("Now at answer: " + answerText);
        });
    });

这将生成以下输出:

Now at question: 1. Question one:
Now at answer: a. Answer one to question one
Now at answer: b. Answer two to question one
Now at answer: c. Answer three to question one

1 个答案:

答案 0 :(得分:1)

经过一番思考,我想出了一个答案。通过用新行分割文档,我们得到一个包含所有行的数组。

然后遍历该数组时,我们只需要确定一行是问题还是答案即可。我已经使用2种不同的正则表达式来做到这一点:

问题:

\d{1,2}\..+

答案:

[a-z]\..+

据此,我们可以决定是否开始一个新问题,或者该行是否需要添加到结果中。

代码可以在下面找到:

// the read document
String document = "Document Name\n" +
    "1. Question one:\n" +
    "a. Answer one to question one\n" +
    "b. Answer two to question one\n" +
    "c. Answer three to question one\n" +
    "2. Question two:\n" +
    "a. Answer one to question two\n" +
    "c. Answer two to question two\n" +
    "e. Answer three to question two";

// splitting by lines
String[] lines = document.split("\r?\n");

// the regex patterns
Pattern questionPattern = Pattern.compile("\\d{1,2}\\..+");
Pattern answerPattern = Pattern.compile("[a-z]\\..+");

// intermediate holding variable
String lastLine = null;

// the result    
Map<String, List<String>> result = new HashMap<>();

for(int lineNumber = 0; lineNumber < lines.length; lineNumber++){
    String line = lines[lineNumber];

    if(questionPattern.matcher(line).matches()){
        result.put(line, new LinkedList<>());
        lastLine = line;
    } else if(answerPattern.matcher(line).matches()){
        result.get(lastLine).add(line);
    } else{
        System.out.printf("Line %s is not a question nor an answer!%n", lineNumber);
    }
}