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.