0

I need help with a java regular expression. I would like to select the answers of a quiz question.

The problem is that the answers can go over several lines with line breaks etc. I managed to select the questions up to 2 lines but not over several lines.

Example of a question

Questions text...bla bla bla bla

A.
This is blank text

B.
That's even more blank text

C.
You know that more
blank text is

D.
blank text
blank text
blank text

There are at least 4 answer possibilities and a maximum of 6.

I've made many attempts - My current result

^[A-G]\.[\s]*^.*$

But only shows the first line after the letter.

^[A-G]\.[\s]*^.*$[\s]+[^. *$]+.* 

However, this only works for responses with several lines.

With Multiline and without Dotall

I would like to make a selection from the letter[A-G] to the blank line. In addition, the answers E and G would only be optional.

search results:
Matching a Line
Multiline

Other experiments were
([A]\..*)([B]\..*)([C]\..*)([D]\..*)
With Dotall - Works well with 4 questions but how can I make E and G optional?

[B]\.[^\n\r]*[\s]*([^\n\r]*\s*[^\n\r]*?\s*[^\n\r]*)?
Just work if the Question has 2 lines.Obviously

Any ideas?

@Java Code

private void getQuestionAnwser(String question) {

        String patternQuestion="^[A-G]\\.[\\s](?:[^\\r\\n]+(?:\\r?\\n|$))*";                
         Pattern pattern = Pattern.compile(patternQuestion,Pattern.MULTILINE);
            Matcher matcher = pattern.matcher(question);
            matcher.matches();
            // check all occurance

            while (matcher.find()) {

                 matcher.start();
                 matcher.end();

                 questionAnwsers.add(matcher.group(0));
                 System.out.println(matcher.group(0));
            }
            String anwser=question.substring(question.indexOf("Answer:"));
            questionAnwsers.add(anwser);

    }

Output :

A.

B.

C.

D.

P.W
  • 3
  • 4

1 Answers1

0

the easiest may be to match until the first empty line or input end

^[A-G]\.[^\n]*\n(?:[^\n]+(?:\n|$))*

and to handle dos line endings

^[A-G]\.[^\r\n]*\r?\n(?:[^\r\n]+(?:\r?\n|$))*

regex101

Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
  • Thanks for the answer. It looks like my text is badly formatted. Instead of a new line, there is first a space and then the new line Unfortunately, this does not seem to work in Java. I tested with the tool [link](https://www.freeformatter.com/java-regex-tester.html#ad-output) – P.W May 03 '18 at 09:41
  • Added Java Code to my Post. But you get the same result with the Link. – P.W May 03 '18 at 10:44
  • indeed your issue is with the `.[\s]` which can match a space or the first newline, the first line can be handled differently : using `*` quantifier (0 or more times , instead of `+` 1 or more), updating answer – Nahuel Fouilleul May 03 '18 at 11:55
  • That's it! Thank you very much – P.W May 03 '18 at 14:32