Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Regular Expressions



  • I am having hard time with Regular Expressions, please help me to write one.
    I have a text file that looks like this

    ...
    N10 (T2 TYPE=BOULE DIAM=12.1 LT=111 LU=80) // capture
    N11 (T3 TYPE=CYL DIAM=10.10 LT=120 LU=88)  // capture
    N12 X5 Y10  Z1 // ignore
    ...
    ...
    

    for every line starting with T<number> i want to construct a Tool object with arguments TYPE, DIAM, LT, LU

    so my Tool object ctor is

    Tool(QString type, double diam, double lt, double lu)
    

    now i need to write the Regular Expression.

    QList<Tool*> tools; // i will put the Tools i create here
    
      const QRegularExpression regx("[T]\d"); // <
    
    QString  currentType;
    double currentDiam;
    double currentLt;
    double currentLu;
      
    
        QTextStream in(&file);
        while(!in.atEnd()) {
            QString line = in.readLine();
            QRegularExpressionMatchIterator iter = regx.globalMatch(line);
            while (iter.hasNext()){
                const QRegularExpressionMatch reMatch = iter.next();
    
       //          if line contains T<number>
       //         currentType = 
       //         currentDiam = 
       //         currentLt = 
       //         currentLu = 
        //   Tool* tool = new Tool(currentLt,currentDiam,currentLt,currentLu  )
       //     tools.append(tool)
      
    

    thank you



  • @LeLev said in Regular Expressions:

    While you wait for someone better/with more time, it's just something like:

    TYPE=(.*) DIAM=(.*) LT=(.*) LU=(.*)
    

    It's the (...) that makes a capture group, which you can reference to pick out what was there.

    I find it useful to play with these at, say, https://regex101.com/, have you tried that?



  • @LeLev said in Regular Expressions:

    While you wait for someone better/with more time, it's just something like:

    TYPE=(.*) DIAM=(.*) LT=(.*) LU=(.*)
    

    It's the (...) that makes a capture group, which you can reference to pick out what was there.

    I find it useful to play with these at, say, https://regex101.com/, have you tried that?



  • @JonB said in Regular Expressions:

    have you tried that?

    yes i was try/fail/retry -ing for 2 houres...

    thank you very much for the quick answer.

    const QRegularExpression regx("(T[0-9][0-9]*) TYPE=(.*) DIAM=([-.0-9]+) LT=([-.0-9]+) LU=([-.0-9]+)");
    

Log in to reply