What I meant in the comments is that if you want to separate numeric values between brackets, you should look directly for them instead of separating first by spaces and then separating numeric values.
In any case, by responding to what you asked, if you want to separate everything by spaces, but ignoring when space is enclosed in square brackets, one solution is that of the code below. The teste1
function does what you request. It uses a feature called " lookahead " negative to check if there is The character ']'
forward (it's the (?!\d+])
part at the end of the regular expression). If it exists, the regular expression does match . In fact this is only a partial solution, to give you an idea of how it works, since it only looks if it has a bracket in front (and there are no brackets behind and front). The problem is that lookbehind does not allow regular subexpressions, so it's hard to do that.
The teste2
function shows you exactly how to search for comma-separated digits that are inside brackets (there, necessarily with one opening and one closing), and ignores the brackets (in ER "[^[]\d+,\s*\d+[^]]"
, parts [^[]
and [^]]
do this - ^
is the symbol for negation when used inside brackets). Then just do the separation ( split ) considering not only the space, but also the comma (this is what ER ",\s*"
does).
#include <iostream>
#include <QString>
#include <QStringList>
#include <QRegularExpression>
using namespace std;
void teste1(const QString &a, const QString &b, const QString &c)
{
QRegularExpression exp = QRegularExpression("\s+(?!\d+])");
QStringList myListA = a.split(exp);
QStringList myListB = b.split(exp);
QStringList myListC = c.split(exp);
cout << "myListA:" << endl;
foreach(QString l, myListA)
cout << " " << l.toStdString() << endl;
cout << "myListB:" << endl;
foreach(QString l, myListB)
cout << " " << l.toStdString() << endl;
cout << "myListC:" << endl;
foreach(QString l, myListC)
cout << " " << l.toStdString() << endl;
}
void teste2(const QString &a, const QString &b, const QString &c)
{
QRegularExpression exp = QRegularExpression("[^[]\d+,\s*\d+[^]]");
QStringList myListA = exp.match(a).capturedTexts();
QStringList myListB = exp.match(b).capturedTexts();
QStringList myListC = exp.match(c).capturedTexts();
cout << "myListA:" << endl;
foreach(QString l, myListA)
if(!l.isEmpty())
{
QStringList values = l.split(QRegularExpression(",\s*"));
cout << "| ";
foreach(QString v, values)
cout << v.toStdString() << " | ";
cout << endl;
}
cout << "myListB:" << endl;
foreach(QString l, myListB)
if(!l.isEmpty())
{
QStringList values = l.split(QRegularExpression(",\s*"));
cout << "| ";
foreach(QString v, values)
cout << v.toStdString() << " | ";
cout << endl;
}
cout << "myListC:" << endl;
foreach(QString l, myListC)
if(!l.isEmpty())
{
QStringList values = l.split(QRegularExpression(",\s*"));
cout << "| ";
foreach(QString v, values)
cout << v.toStdString() << " | ";
cout << endl;
}
}
int main()
{
QString a = "strength 0.5";
QString b = "'kernels[1].params.sigma_albedo' 0.02";
QString c = "debugPixel '[336, 209]'";
cout << "TESTE 1 -------------------" << endl;
teste1(a, b, c);
cout << endl;
cout << "TESTE 2 -------------------" << endl;
teste2(a, b, c);
return 0;
}
Result:
TESTE 1 -------------------
myListA:
strength
0.5
myListB:
'kernels[1].params.sigma_albedo'
0.02
myListC:
debugPixel
'[336, 209]'
TESTE 2 -------------------
myListA:
myListB:
myListC:
| 336 | 209 |