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. QRegularExpression and QString::replace()
Forum Update on Monday, May 27th 2025

QRegularExpression and QString::replace()

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 18.5k 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.
  • _ Offline
    _ Offline
    _Mark_
    wrote on last edited by
    #1

    Hi,
    I have this QString:

    @name="abc" blabla="xyz"
    name='abc' blabla='uvw'
    name=abc blabla='uvw'
    @

    I want to replace the item abc with something else, let's say with foo.

    Using regular expressions I'm able to catch the three cases (no/single/double quotes), for example:

    @re = ""(abc)"|'(abc)'|(abc)";
    @
    Now I want to replace them with foo, maintaining their syntax.
    But if I try with:

    @
    myString.replace(QRegular[removed]re), "foo");
    @

    it replaces not only the captured text but also the quotes (if any).
    How to avoid that?

    Thanks!
    Mark

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Maybe I'm missing something, but why not just
      @
      myString.replace("abc", "foo");
      @

      1 Reply Last reply
      0
      • _ Offline
        _ Offline
        _Mark_
        wrote on last edited by
        #3

        Because what if abc is contained in another word?

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Then your regexp won't help either, unless you extend it to consider spaces etc.

          You can capture what's before and after the "abc" with separate groups and use them in replacing string:
          @
          QRegularExpression re("("|'|=|\s)abc("|'|\s)");
          str.replace(re, "\1foo\2")
          @

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JvdGlind
            wrote on last edited by
            #5

            @#include <QRegularExpression>
            #include <QDebug>

            int main(int argc, char *argv[])
            {
            QRegularExpression regex(QStringLiteral("(?<=["'=])abc(?=["' ]|$)"));

            QString first("name=\"abc\"");
            QString second("name='abc'");
            QString third("name=abc");
            QString thirdWithSpace("name=abc ");
            QString noMatch("blabcla");
            QString noMatchThird("name=abcc");
            
            qDebug() << regex.match(first);
            qDebug() << regex.match(second);
            qDebug() << regex.match(third);
            qDebug() << regex.match(thirdWithSpace);
            qDebug() << regex.match(noMatch);
            qDebug() << regex.match(noMatchThird);
            
            QString replace("foo");
            
            qDebug() << first.replace(regex, replace);
            qDebug() << second.replace(regex, replace);
            qDebug() << third.replace(regex, replace);
            qDebug() << thirdWithSpace.replace(regex, replace);
            qDebug() << noMatch.replace(regex, replace);
            qDebug() << noMatchThird.replace(regex, replace);
            
            return 0;
            

            }
            @

            Using lookahead and lookbehind I managed to create this.

            Jeffrey VAN DE GLIND
            Principle Consultant @ Nalys
            www.nalys-group.com

            1 Reply Last reply
            0
            • _ Offline
              _ Offline
              _Mark_
              wrote on last edited by
              #6

              Interesting! Thanks both.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                bipll
                wrote on last edited by
                #7

                [quote]Because what if abc is contained in another word? [/quote]
                @re = "<abc>";@

                ?

                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