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. QString: replace first and second appear string
Forum Updated to NodeBB v4.3 + New Features

QString: replace first and second appear string

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 2.3k Views 2 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1
    Replace first "~~" to "<s>", second "~~" to "</s>".
    Example:
    ~~Delete~~
    To
    <s>Delete</s>
    

    https://github.com/sonichy

    JonBJ 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by sierdzio
      #2

      Is there a question somewhere in here?

      There is no magic API that will do it for you, you need to come up with an algorithm to replace such strings. An example (probably slow):

      const QLatin1String delimiter("~~");
      QString string("~~Delete~~");
      bool odd = false;
      while (string.contains(delimiter)) {
        if (odd) {
          string.replace(delimiter, "</s>");
        } else {
          string.replace(delimiter, "<s>");
        }
        odd != odd;
      }
      

      (Z(:^

      1 Reply Last reply
      10
      • sonichyS sonichy
        Replace first "~~" to "<s>", second "~~" to "</s>".
        Example:
        ~~Delete~~
        To
        <s>Delete</s>
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @sonichy , @sierdzio
        I'm going to throw my hat in, and ask a question.

        Although the OP may not want to use regular expressions to do this, and @sierdzio's solution is doubtless perfectly acceptable, I would. It intrigued me, so I had a go (e.g. use https://regex101.com/, highly recommended). In preparation for the substitution on ~~Delete~~ I got as far as:

        (~~)((?!~~).*)(~~)
        

        I'm assuming I need the negative lookahead construct (?!~~) to prevent gobbling any middling ~~s before the next one. My expression is not right though.

        It's very difficult to Google for, because it's so hard to describe. Would an re expert care to show what the correct re for the match/substitution would be here, please?

        EDIT: Just thought it through again. Is this to be solved simply via the non-greedy construct .*? :

        ~~(.*?)~~
        

        and then we can just replace globally with <s>\1</s> , doing the lot in one go?

        So I think (untested):

        QString s = "000~~abc~~~~Delete~~def~~ghi~~999";
        s.replace(QRegularExpression("~~(.*?)~~"), "<s>\\1</s>");
        
        sonichyS 1 Reply Last reply
        6
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Yes, regular expressions are a good way to solve this as well, and your example seems good (I have not tested it, though).

          One thing I dislike about regexp is that it is "write-only" code - very hard to understand and maintain later. It is a valid solution, and will probably work faster than my attempt.

          (Z(:^

          JonBJ 1 Reply Last reply
          3
          • sierdzioS sierdzio

            Yes, regular expressions are a good way to solve this as well, and your example seems good (I have not tested it, though).

            One thing I dislike about regexp is that it is "write-only" code - very hard to understand and maintain later. It is a valid solution, and will probably work faster than my attempt.

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

            @sierdzio
            You are correct that they can be a bit hard to understand. OTOH, I have now posted a one-line solution, while yours is (understandably) many lines to work through. Horses for courses... :)

            1 Reply Last reply
            1
            • JonBJ JonB

              @sonichy , @sierdzio
              I'm going to throw my hat in, and ask a question.

              Although the OP may not want to use regular expressions to do this, and @sierdzio's solution is doubtless perfectly acceptable, I would. It intrigued me, so I had a go (e.g. use https://regex101.com/, highly recommended). In preparation for the substitution on ~~Delete~~ I got as far as:

              (~~)((?!~~).*)(~~)
              

              I'm assuming I need the negative lookahead construct (?!~~) to prevent gobbling any middling ~~s before the next one. My expression is not right though.

              It's very difficult to Google for, because it's so hard to describe. Would an re expert care to show what the correct re for the match/substitution would be here, please?

              EDIT: Just thought it through again. Is this to be solved simply via the non-greedy construct .*? :

              ~~(.*?)~~
              

              and then we can just replace globally with <s>\1</s> , doing the lot in one go?

              So I think (untested):

              QString s = "000~~abc~~~~Delete~~def~~ghi~~999";
              s.replace(QRegularExpression("~~(.*?)~~"), "<s>\\1</s>");
              
              sonichyS Offline
              sonichyS Offline
              sonichy
              wrote on last edited by
              #6

              @JonB

              It is hard to understand, what is the meaning of \\1 
              

              https://github.com/sonichy

              aha_1980A 1 Reply Last reply
              0
              • sonichyS sonichy

                @JonB

                It is hard to understand, what is the meaning of \\1 
                
                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @sonichy you will need to learn som regex basics.

                \1 is the string captured between ( and ), and in C++ you have to escape the backslash, hence \\1.

                Qt has to stay free or it will die.

                sonichyS 1 Reply Last reply
                1
                • Kent-DorfmanK Offline
                  Kent-DorfmanK Offline
                  Kent-Dorfman
                  wrote on last edited by Kent-Dorfman
                  #8

                  This is exactly what regular expressions are for...Qt may be overkill if OP is just processing a markup text document.

                  simplest perl example, not meant to be a comprehensive solution:
                  echo "something ~~token~~ more" | perl -ne "s/(~~)(.*)(~~)/<s>\$2<\/s>/; print;"

                  output:
                  something <s>token</s> more

                  Sorry if mentioning a different tool is blasphemy. LOL

                  1 Reply Last reply
                  5
                  • aha_1980A aha_1980

                    @sonichy you will need to learn som regex basics.

                    \1 is the string captured between ( and ), and in C++ you have to escape the backslash, hence \\1.

                    sonichyS Offline
                    sonichyS Offline
                    sonichy
                    wrote on last edited by
                    #9

                    @aha_1980 Thank you !

                    https://github.com/sonichy

                    JonBJ 1 Reply Last reply
                    1
                    • sonichyS sonichy

                      @aha_1980 Thank you !

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

                      @sonichy
                      As you please. With due respect to @Kent-Dorfman's suggestion, apart from the fact that it's written in Perl it will not correctly deal with multiple required substitutions on any given line. If you wish to try a regular expression for your situation I humbly suggest you try the correct Qt one I gave earlier, with its multi-substitution example I provided.

                      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