Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Find sequence in QString
Qt 6.12 LTS is out! See what's new in the release blog

Find sequence in QString

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.5k Views 3 Watching
  • 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.
  • beeckscheB Offline
    beeckscheB Offline
    beecksche
    wrote on last edited by
    #1

    Hi,

    In a .txt file i need to find a specific sequence in a line.

    The lines which i need to detect are:

      0: -0,385236
      1: 0,141017
      2: 4,39365
      3: 12,0689
      4: 7,32349
      5: -4,69991
      6: -1,30162
    

    Sequence: Space - Integer - Double Point - Space - Float Number

    I started to read each line of the file. But i have no clue how to detect this sequence.

    QFile file("file.txt");
    if (!file.open()) {
       QTextStream stream(&file);
       forever {   
          QString line = stream.readlLine();
          if (line.isNull())
             break;
    
        // now detect the sequence
       }
    }
    

    I'm not sure if i can use the QRegExp class.

    Thanks for help!

    raven-worxR 1 Reply Last reply
    0
    • beeckscheB beecksche

      Hi,

      In a .txt file i need to find a specific sequence in a line.

      The lines which i need to detect are:

        0: -0,385236
        1: 0,141017
        2: 4,39365
        3: 12,0689
        4: 7,32349
        5: -4,69991
        6: -1,30162
      

      Sequence: Space - Integer - Double Point - Space - Float Number

      I started to read each line of the file. But i have no clue how to detect this sequence.

      QFile file("file.txt");
      if (!file.open()) {
         QTextStream stream(&file);
         forever {   
            QString line = stream.readlLine();
            if (line.isNull())
               break;
      
          // now detect the sequence
         }
      }
      

      I'm not sure if i can use the QRegExp class.

      Thanks for help!

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @beecksche
      use QRegularExpression:

      QRegularExpression rx("([0-9])+:\\s(\\-?[0-9]+,[0-9]+)");
      QRegularExpressionMatch match = rx.match( line ); // line == "5: -4,69991"
      if (match.hasMatch())
      {
          QString id = match.captured(0); // id == "5"
          QString value = match.captured(1); // value == "-4,69991"
          // ...
      }
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      beeckscheB 1 Reply Last reply
      5
      • raven-worxR raven-worx

        @beecksche
        use QRegularExpression:

        QRegularExpression rx("([0-9])+:\\s(\\-?[0-9]+,[0-9]+)");
        QRegularExpressionMatch match = rx.match( line ); // line == "5: -4,69991"
        if (match.hasMatch())
        {
            QString id = match.captured(0); // id == "5"
            QString value = match.captured(1); // value == "-4,69991"
            // ...
        }
        
        beeckscheB Offline
        beeckscheB Offline
        beecksche
        wrote on last edited by beecksche
        #3

        @raven-worx

        Thanks a lot!

        In the captured() function i had to change the argument:

                QString id = match.captured(1); // id == "5"
                QString value = match.captured(2); // value == "-4,69991"
        

        Edit:

        To use it, i also had to change the expressio to:

        QRegularExpression rx("([0-9]+):\\s(\\-?[0-9]+,[0-9]+)");
        

        to get the first integers bigger the 10.

        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
        • Unsolved