Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Regular Expressions
Forum Updated to NodeBB v4.3 + New Features

Regular Expressions

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 273 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by ODБOï
    #1

    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

    JonBJ 1 Reply Last reply
    0
    • ODБOïO ODБOï

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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?

      ODБOïO 1 Reply Last reply
      5
      • JonBJ JonB

        @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?

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by ODБOï
        #3

        @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]+)");
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved