In your example all the lines you wish to capture start with "-- " and end with "(L.)" where "." is a wildcard, and the unwanted grouping lines do not. Amleto is asking if that is a rule (or something like a rule) you can use. If amleto's observation is correct then this:
#include <QtCore>
#include <QDebug>
int main(int argc, char **argv)
{
while (!s.atEnd()) {
if (re.exactMatch(line))
qDebug() << re.cap(1);
}
}
app.exec();
}
#include <QtCore>
#include <QDebug>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QFile in("test.txt");
if (in.open(QIODevice::ReadOnly)) {
QTextStream s(&in);
QRegExp re("--\\s.*\\((L.)\\)");
while (!s.atEnd()) {
QString line = s.readLine();
if (re.exactMatch(line))
qDebug() << re.cap(1);
}
}
app.exec();
}
To copy to clipboard, switch view to plain text mode
trivially gives:
"L1"
"L2"
"L3"
"L6"
"L2"
"L9"
"L0"
"LL"
"L1"
"L2"
"L3"
"L6"
"L2"
"L9"
"L0"
"LL"
To copy to clipboard, switch view to plain text mode
If not, you need to infer hierarchy from changes in the amount of whitespace between "-- " and the text of lines ending "\\(.+\\)". Something like:
#include <QtCore>
#include <QDebug>
int main(int argc, char **argv)
{
int lastDepth = 0;
QRegExp re
("--(\\s+).*\\((.+)\\)");
while (!s.atEnd()) {
if (re.exactMatch(line)) {
int newDepth = re.cap(1).length();
if (lastDepth > 0 && newDepth <= lastDepth)
result << lastValue;
lastDepth = newDepth;
lastValue = re.cap(2);
}
else if (line.startsWith("---")) {
if (lastDepth > 0)
result << lastValue;
break;
}
}
qDebug() << result;
}
app.exec();
}
#include <QtCore>
#include <QDebug>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QFile in("test.txt");
if (in.open(QIODevice::ReadOnly)) {
QTextStream s(&in);
int lastDepth = 0;
QString lastValue;
QStringList result;
QRegExp re("--(\\s+).*\\((.+)\\)");
while (!s.atEnd()) {
QString line = s.readLine();
if (re.exactMatch(line)) {
int newDepth = re.cap(1).length();
if (lastDepth > 0 && newDepth <= lastDepth)
result << lastValue;
lastDepth = newDepth;
lastValue = re.cap(2);
}
else if (line.startsWith("---")) {
if (lastDepth > 0)
result << lastValue;
break;
}
}
qDebug() << result;
}
app.exec();
}
To copy to clipboard, switch view to plain text mode
outputs:
("L1", "L2", "L3", "L6", "L2", "L9", "L0", "LL")
("L1", "L2", "L3", "L6", "L2", "L9", "L0", "LL")
To copy to clipboard, switch view to plain text mode
for your example. This is dependent on reliable indenting which, if humans are involved, is unlikely to be the case.
Bookmarks