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. Finding exact match for QString in a QStringList
Forum Updated to NodeBB v4.3 + New Features

Finding exact match for QString in a QStringList

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 3.4k 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.
  • Vinod KuntojiV Offline
    Vinod KuntojiV Offline
    Vinod Kuntoji
    wrote on last edited by
    #1

    I have stringlist,

    [marking_property,property,prop,props]

    Here, I want to replace, property with ID,
    I expect, the stringlist should be

    [marking_property,ID,prop,props]
    How do I do this using QRegularExpression?

    C++, Qt, Qt Quick Developer,
    PthinkS, Bangalore

    1 Reply Last reply
    0
    • aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Vinod-Kuntoji:

      Hi, have you tried http://doc.qt.io/qt-5/qstringlist.html#replaceInStrings-2 ? It contains a small example and seems to do exactly what you want.

      Qt has to stay free or it will die.

      1 Reply Last reply
      1
      • Vinod KuntojiV Offline
        Vinod KuntojiV Offline
        Vinod Kuntoji
        wrote on last edited by
        #3

        @aha_1980 ,

        QString s = "Some text MyWord some more text MyWordAgain"
        s.replace("MyWord","AnotherWord")
        Now I want to replace occurrences of 'MyWord' only and 'MyWordAgain' would not be replaced.

        C++, Qt, Qt Quick Developer,
        PthinkS, Bangalore

        jsulmJ 1 Reply Last reply
        0
        • Vinod KuntojiV Vinod Kuntoji

          @aha_1980 ,

          QString s = "Some text MyWord some more text MyWordAgain"
          s.replace("MyWord","AnotherWord")
          Now I want to replace occurrences of 'MyWord' only and 'MyWordAgain' would not be replaced.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Vinod-Kuntoji You can use a regular expression with http://doc.qt.io/qt-5/qstring.html#replace-12

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • Vinod KuntojiV Offline
            Vinod KuntojiV Offline
            Vinod Kuntoji
            wrote on last edited by Vinod Kuntoji
            #5

            @jsulm ,

            I tried like this,

                QFile data(filename);
                data.open(QIODevice::Text | QIODevice::ReadOnly);
                QString dataText = data.readAll();
            
                QRegularExpression re(oldTag);
                QString replacementText(newTag);
                if(!dataText.contains(re)){
                    qDebug() << "String Not Found.." << endl;
                    return;
                }
                dataText.replace(re, replacementText);
                QFile newData(filename);
                if(newData.open(QFile::WriteOnly | QFile::Truncate)) {
                    QTextStream out(&newData);
                    out << dataText;
                }
                newData.close();
            

            But it is not replacing the exact string.

            C++, Qt, Qt Quick Developer,
            PthinkS, Bangalore

            jsulmJ 1 Reply Last reply
            0
            • Vinod KuntojiV Vinod Kuntoji

              @jsulm ,

              I tried like this,

                  QFile data(filename);
                  data.open(QIODevice::Text | QIODevice::ReadOnly);
                  QString dataText = data.readAll();
              
                  QRegularExpression re(oldTag);
                  QString replacementText(newTag);
                  if(!dataText.contains(re)){
                      qDebug() << "String Not Found.." << endl;
                      return;
                  }
                  dataText.replace(re, replacementText);
                  QFile newData(filename);
                  if(newData.open(QFile::WriteOnly | QFile::Truncate)) {
                      QTextStream out(&newData);
                      out << dataText;
                  }
                  newData.close();
              

              But it is not replacing the exact string.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by jsulm
              #6

              @Vinod-Kuntoji Well, if you only want to replace whole words then you need something like this as regular expression: "\s+MyWord\s+" you cannot just use MyWord as regular expression and expect it to only replace whole words.
              You can use this to test regular expressions: https://regex101.com/

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              JonBJ 1 Reply Last reply
              1
              • jsulmJ jsulm

                @Vinod-Kuntoji Well, if you only want to replace whole words then you need something like this as regular expression: "\s+MyWord\s+" you cannot just use MyWord as regular expression and expect it to only replace whole words.
                You can use this to test regular expressions: https://regex101.com/

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

                @jsulm
                The problem with \s+MyWord\s+ is that it requires whitespace on either side, so for example if the complete string is only MyWord it won't match.

                Most regular expressions syntaxes these days allow \bMyWord\b where \b is a non-eat-character "word-boundary" match. I think Qt is saying it allows Perl-flavor regexps, and that supports \b (and \B).

                jsulmJ 1 Reply Last reply
                1
                • JonBJ JonB

                  @jsulm
                  The problem with \s+MyWord\s+ is that it requires whitespace on either side, so for example if the complete string is only MyWord it won't match.

                  Most regular expressions syntaxes these days allow \bMyWord\b where \b is a non-eat-character "word-boundary" match. I think Qt is saying it allows Perl-flavor regexps, and that supports \b (and \B).

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @JNBarchan It was just an example. The exact expression depends on exact needs.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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