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. How to search and replace a substring ?
Forum Update on Tuesday, May 27th 2025

How to search and replace a substring ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 19.5k 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.
  • R Offline
    R Offline
    rmam
    wrote on last edited by
    #1

    I'm looking for a way to replace any sub-string present in a QString object that is included in a hard-coded list of strings which include dozens of entries. Right now I'm passing QString::replace a QRegularExpression that covers all matching strings. However, I'm not sure this is an adequate way of doing things.

    Does anyone know what's the best way to pull this off?

    aha_1980A JonBJ 2 Replies Last reply
    0
    • R rmam

      I'm looking for a way to replace any sub-string present in a QString object that is included in a hard-coded list of strings which include dozens of entries. Right now I'm passing QString::replace a QRegularExpression that covers all matching strings. However, I'm not sure this is an adequate way of doing things.

      Does anyone know what's the best way to pull this off?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @rmam,

      I have seen implementations that simply nest calls to QString::replace(QString, QString):

      QString s = "The quick brown fuchs jumps ueber the lazy hund.";
      s.replace("fuchs", "fox").replace("ueber", "over").replace("hund", "dog");
      
      

      However, this involves multiple string allocations so it might be slower than a regular expression based replace. Or it might be faster. The only thing you can do is benchmark with your original data.

      Happy New Year!

      Qt has to stay free or it will die.

      1 Reply Last reply
      0
      • R rmam

        I'm looking for a way to replace any sub-string present in a QString object that is included in a hard-coded list of strings which include dozens of entries. Right now I'm passing QString::replace a QRegularExpression that covers all matching strings. However, I'm not sure this is an adequate way of doing things.

        Does anyone know what's the best way to pull this off?

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

        @rmam
        What is the replacement string in these cases? E.g. is it "", so you are removing them all, or something else? This will affect possible solutions....

        T R 2 Replies Last reply
        1
        • JonBJ JonB

          @rmam
          What is the replacement string in these cases? E.g. is it "", so you are removing them all, or something else? This will affect possible solutions....

          T Offline
          T Offline
          Tavit
          wrote on last edited by
          #4

          Hi @JonB
          Above suggested code is fine,but regular expression based replace is good only when the involvement of special symbol replacements.

          Thanks and Regards,
          Tavit.

          JonBJ 1 Reply Last reply
          0
          • T Tavit

            Hi @JonB
            Above suggested code is fine,but regular expression based replace is good only when the involvement of special symbol replacements.

            Thanks and Regards,
            Tavit.

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

            @Tavit
            Don't know what your post means. I haven't suggested anything yet: I am asking the OP what his replacement actually is (e.g. is he just removing the search strings completely, or putting something else in?)

            T 1 Reply Last reply
            0
            • JonBJ JonB

              @Tavit
              Don't know what your post means. I haven't suggested anything yet: I am asking the OP what his replacement actually is (e.g. is he just removing the search strings completely, or putting something else in?)

              T Offline
              T Offline
              Tavit
              wrote on last edited by
              #6

              @JonB
              Sorry got overlooked the post. My opinion is what ever he is used the code that is right. In terms of replacing the strings.

              QString s = "The quick brown fuchs jumps ueber the lazy hund.";
              s.replace("fuchs", "fox").replace("ueber", "over").replace("hund", "dog");
              

              Based on @rmam post he has to replace the search string with another specific string.

              JonBJ 1 Reply Last reply
              0
              • T Tavit

                @JonB
                Sorry got overlooked the post. My opinion is what ever he is used the code that is right. In terms of replacing the strings.

                QString s = "The quick brown fuchs jumps ueber the lazy hund.";
                s.replace("fuchs", "fox").replace("ueber", "over").replace("hund", "dog");
                

                Based on @rmam post he has to replace the search string with another specific string.

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

                @Tavit

                Based on @rmam post he has to replace the search string with another specific string.

                If that is indeed the case, then using multiple/chained replace()s as you suggest is not the correct method to do it, and may lead to incorrect replacements. That's why we need to hear back from the OP....

                1 Reply Last reply
                0
                • JonBJ JonB

                  @rmam
                  What is the replacement string in these cases? E.g. is it "", so you are removing them all, or something else? This will affect possible solutions....

                  R Offline
                  R Offline
                  rmam
                  wrote on last edited by
                  #8

                  @JonB the replacement would be a slightly altered version of the original keyword. For instance, consider that the regex tries to match instances of keyword "foo" that occur only after "this.foo." then the goal is to replace them with, for example "this <bar>foo</bar>."

                  JonBJ 1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    Is it some sort of template replacement ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • R rmam

                      @JonB the replacement would be a slightly altered version of the original keyword. For instance, consider that the regex tries to match instances of keyword "foo" that occur only after "this.foo." then the goal is to replace them with, for example "this <bar>foo</bar>."

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

                      @rmam
                      You can't use multiple replace()s if there is any chance that the replacement string could be found by a search string.

                      Once you start specifying patterns like you are now, you're better off with regular expressions, and you don't have the potential issue of one replacement causing another. So actually your current solution may be good. You can build up the many strings dynamically before passing them as reg exp if that's better to maintain in code, I don't know if Qt has any limits on length of pattern.

                      Now, of course once @SGaist has spoken it's worth answering him, as he might have a tailored solution :)

                      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