Привет. У меня возникли проблемы при попытке сопоставить выбранный текст с помощью Matcher. Позвольте мне объяснить с некоторыми примерами:
Вот текст, который я просматриваю: (Если это поможет, я сгенерировал это из некоторого XML, поэтому исходные данные выглядели не так, как я. надо работать с)
__ELEMENT: question
____Text: (a) Look at the following:
__ELEMENT: maths
____Text: x
__ELEMENT: maths
____Text: x^2
__ELEMENT: maths
____Text: x^3
__ELEMENT: question
____Text: Then do this blah blah blah.
__ELEMENT: question
____Text: (b) Hence do the other thing blah blah blah.
__ELEMENT: maths
____Text: x^4
__ELEMENT: maths
____Text: x^5
Поэтому я пытаюсь найти текст, следующий за «вопросом» и всеми последующими математическими элементами; Я имею в виду, что я хотел бы получить:
(a) Look at the following:
x x^2 x^3
Then do this blah blah blah.
(b) Hence do the other thing blah blah blah.
x^4 x^5
Но все, что я пробовал до сих пор, только что дало бесполезные вещи, как:
(a) Look at the following:
x x^2 x^3 x^4 x^5
Then do this blah blah blah.
(b) Hence do the other thing blah blah blah.
x x^2 x^3 x^4 x^5
Или хуже
Вот соответствующий бит кода:
string text = "__ELEMENT: question\n" +
"____Text: (a) Look at the following:\n" +
"__ELEMENT: maths\n" +
"____Text: x\n" +
"__ELEMENT: maths\n" +
"____Text: x^2\n" +
"__ELEMENT: maths\n" +
"____Text: x^3\n" +
"__ELEMENT: question\n" +
"____Text: Then do this blah blah blah.\n" +
"__ELEMENT: question\n" +
"____Text: (b) Hence do the other thing blah blah blah.\n" +
"__ELEMENT: maths\n" +
"____Text: x^4\n" +
"__ELEMENT: maths\n" +
"____Text: x^5";
Pattern p = Pattern.compile("(?<=question\\n.*Text:).+");
Matcher m = p.matcher(text);
if (m.find()) {
String findQuestion = m.group(0);
String newQuestion = findQuestion;
while (m.find()) {
Pattern pat = Pattern.compile("maths\\n_*Text: (.*(?:\\n(?!_*ELEMENT).*)*)\\n");
Matcher mat = pat.matcher(text);
if (mat.find()) {
String findMath = mat.group(1);
String newMath = findEquationQ;
while (mat.find()) {
newQuestion += mat.group(1);
}
finalMatches.add(newQuestion);
}
}
// finalMatches.add(newQuestion);
}
Да, это беспорядок
Любая помощь будет оценена Или просто какой-нибудь лог c или псевдокод, чтобы помочь