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. [SOLVED] how to use QRegExp to find some continue spaces?
QtWS25 Last Chance

[SOLVED] how to use QRegExp to find some continue spaces?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 5.0k 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.
  • S Offline
    S Offline
    silent_missile
    wrote on last edited by
    #1

    I have a string
    @
    178542 5 5 5 0 1447 33386 1452 1452 44585 44631 1452 44576
    @
    in the sample it has 13 sections, in the real work, it may contain different sections

    in the sample, one section contains 8 characters, and each section starts by space including the first section, so the string starts by space.

    I need to output a string in the same format, so I need to find how long each section is.

    I tried to use QRegExp, my idea is that: the start position of the 2nd continue spaces is the length of 1st section. Then I tried:
    @
    position=QRegExp("\s+").indexIn(str,1);
    @
    but I failed, it returns 0 which is the start of the 1st continue spaces. Then I tried to use
    @
    qDebug()<<rx.pos();
    @
    to find the 2nd/3rd/4th match, but every one returns -1 which means no match.

    I feel confused, it seems that QRegExp works not the same as other regular expression tool. Can any one help me?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      Why not simply do:
      @QStringList list = str.split(" ", QString::SkipEmptyParts);@

      Now you have a list containing only the parts between the spaces, so you can easily walk through the list and check QString::length() of each part...

      I think the same can be done with with RegExp, but I would use QRegExp("\S+") with an upper case S, which matches only characters that are not whitespaces. Then you could do:
      @QRegExp reg("\S+")
      int pos = reg.indexIn(str, 0);
      while(pos >= 0)
      {
      int len = reg.cap(0).length(); //This returns the length of each part
      pos = reg.indexIn(str, pos+1);
      }@

      Still I think QString::split() is more straight forward here...

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        So you have numbers right justified in fixed width columns and you want to work out the widths of the columns, which includes the leading spaces.

        @
        QString line(" 178542 5 5 5 0 1447 33386 1452 1452 44585 44631 1452 44576");

        QList<int> widths;
        QRegExp re("\s+\S+");
        int pos = 0;
        while ((pos = re.indexIn(line, pos)) != -1) {
        widths << re.matchedLength();
        pos += re.matchedLength();
        }
        qDebug() << widths;
        // (12, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8)
        @

        Of course, it won't work if some of the columns are "empty" or if the column can contain spaces.

        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