Regular expression help

1

Hello, I need a help with RegEx. I need to do a split of a string to store in a list, so I'm doing so.

QString a = "strength 0.5";
QString b = "'kernels[1].params.sigma_albedo' 0.02";
QString c = "debugPixel '[336, 209]'";

QStringList myListA = a.split(QRegularExpression("\s+"));
QStringList myListB = b.split(QRegularExpression("\s+"));
QStringList myListC = c.split(QRegularExpression("\s+"));

I need to separate by the blanks, but I'll soon separate the '[336, 209]' parameter into two parts and need to be together: I even managed to do the following RegEx (\ '[\ d +, \ s \ d +] \') but it is not right. I really need to select all the blanks except the blanks that are within []. Does anyone know how to do this? I know I can do it with loops and tests in the string, but I want to work with RegEx and also to better understand how it works.

myListA:
myList.at(0); // strength
myList.at(1); // 0.5

myListB:
myList.at(0); // 'kernels[1].params.sigma_albedo'
myList.at(1); // 0.2

// Errado
myListC: 
myList.at(0); // debugPixel
myList.at(1); // '[336,
myList.at(2); // 209]'

// Certo
myListC: 
myList.at(0); // debugPixel
myList.at(1); // '[336, 209]'
    
asked by anonymous 07.03.2017 / 02:31

1 answer

1

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 |
    
07.03.2017 / 05:11