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. Separate QString by QRegex
Qt 6.11 is out! See what's new in the release blog

Separate QString by QRegex

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.8k Views 1 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    [code]
    QString contents = "lll-###";

    QRegExp split("\b");

    QStringList splitContents = contents.split(split, QString::SkipEmptyParts);
    for(QString const &data : splitContents)
    {
    qDebug() << data;
    }
    [/code]

    The result is "lll", "-###"
    How could I told the QReg if I want the result become
    "lll-", "###"

    I want to make "#+"(I mean "#", "##", "###" and so on) as word boundary

    example
    "aaabbb%### ddd###" will be
    "aaabbb%", "###", " ddd", "###"

    Thanks

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      [QUOTE]1) work around it.[/QUOTE]
      My pretty nasty work around

      [code]
      //helper function
      std::list<std::pair<int, int> > get_positions(QRegExp const &rx, QString const &contents)
      {
      int pos = 0;
      std::list<std::pair<int, int> > result;
      while((pos = rx.indexIn(contents, pos)) != -1)
      {
      //qDebug() << pos2 << ", "<< rx.matchedLength();
      result.push_back(std::make_pair(pos, rx.matchedLength()));
      pos += rx.matchedLength();
      }

      return result;
      }

      //helper function
      template<typename T>
      QStringList const split_str_by_positions(T const &position, QString const &contents)
      {
      QStringList results;
      for(std::pair<int, int> const &data : position)
      {
      results << contents.mid(data.first, data.second);
      }

      return results;
      

      }

      //split the contents into the QStringList
      void test_get_positions()
      {
      QString contents = "lll ### ### ###";

      typedef std::pair<int, int> DInt;
      std::list<DInt> positions = get_positions(QRegExp("#+"), contents);
      
      std::list<DInt> positions2 = get_positions(QRegExp("[^#]+"), contents);
      
      struct DIntCompare
      {
          bool operator()(DInt const &one, DInt const &two) const
          { return one.first < two.first; }
      };
      
      positions.merge(positions2, DIntCompare());
      
      qDebug() << "---------------------";
      
      for(DInt const &data : positions) qDebug() << data.first <<", "<<data.second;
      
      QStringList results = split_str_by_positions(positions, contents);
      
      for(QString const &data : results) qDebug() << data;
      

      }
      [/code]

      Do you have other easier solutions?
      My workaround is ugly, want to find other better way to implement it.
      Thanks

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mario84
        wrote on last edited by
        #3

        you could just call contents.split("###"), but you won't get "###" in your resulting list.
        (Which shouldn't be a big deal, just add them again afterwards if you need them...)
        Try something like that:

        [code]
        QString contents = "lll-###";

        QString split("###");

        QStringList splitContents = contents.split(split, QString::SkipEmptyParts);

        for (int i = splitContents.count(); i >= 0; i--)
        {
        splitContents.insert(i+1, split);
        }

        for(QString const &data : splitContents)
        {
        qDebug() << data;
        }
        [/code]

        if this solution isn't flexible enough for your purposes, you'll probably have to write your own parser...

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Not really sure what you want to achieve, but maybe use the "\b-" as QRegExp? When I test it, it gives me two strings in the splitContents. "lll" and "###". I lost the "-" ofcourse of the QString.split function.
          The correct Qt way for the for () is more:
          @ QString contents = "lll-###";

           QRegExp split("\\b-");
          
           QStringList splitContents = contents.split(split, QString::SkipEmptyParts);
           foreach (QString const &data , splitContents)
           {
               qDebug() << data;
           }
          

          @

          Greetz, Jeroen

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            bq. Not really sure what you want to achieve

            I want to implement the rename function like the "XnView" did

            example : aaa_###$##$#
            will produce strings like
            "aaa_000$00$0"
            "aaa_001$01$1" and so on
            it would only replace "#" to integer

            The most easiest solution of mine is
            (1)split the string into
            aaa_,###, $,##, $, #

            (2)concatenate those string and replace "#" to number

            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