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.
  • S Offline
    S Offline
    sonichy
    wrote on 24 Apr 2019, 10:08 last edited by
    #1
    Replace first "~~" to "<s>", second "~~" to "</s>".
    Example:
    ~~Delete~~
    To
    <s>Delete</s>
    

    https://github.com/sonichy

    J 1 Reply Last reply 24 Apr 2019, 11:29
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 24 Apr 2019, 10:18 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
      • S sonichy
        24 Apr 2019, 10:08
        Replace first "~~" to "<s>", second "~~" to "</s>".
        Example:
        ~~Delete~~
        To
        <s>Delete</s>
        
        J Online
        J Online
        JonB
        wrote on 24 Apr 2019, 11:29 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>");
        
        S 1 Reply Last reply 28 Apr 2019, 03:28
        6
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 24 Apr 2019, 11:45 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(:^

          J 1 Reply Last reply 24 Apr 2019, 11:50
          3
          • S sierdzio
            24 Apr 2019, 11:45

            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.

            J Online
            J Online
            JonB
            wrote on 24 Apr 2019, 11:50 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
            • J JonB
              24 Apr 2019, 11:29

              @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>");
              
              S Offline
              S Offline
              sonichy
              wrote on 28 Apr 2019, 03:28 last edited by
              #6

              @JonB

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

              https://github.com/sonichy

              A 1 Reply Last reply 28 Apr 2019, 04:58
              0
              • S sonichy
                28 Apr 2019, 03:28

                @JonB

                It is hard to understand, what is the meaning of \\1 
                
                A Offline
                A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on 28 Apr 2019, 04:58 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.

                S 1 Reply Last reply 30 Apr 2019, 02:23
                1
                • Kent-DorfmanK Offline
                  Kent-DorfmanK Offline
                  Kent-Dorfman
                  wrote on 28 Apr 2019, 05:06 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
                  • A aha_1980
                    28 Apr 2019, 04:58

                    @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.

                    S Offline
                    S Offline
                    sonichy
                    wrote on 30 Apr 2019, 02:23 last edited by
                    #9

                    @aha_1980 Thank you !

                    https://github.com/sonichy

                    J 1 Reply Last reply 30 Apr 2019, 11:50
                    1
                    • S sonichy
                      30 Apr 2019, 02:23

                      @aha_1980 Thank you !

                      J Online
                      J Online
                      JonB
                      wrote on 30 Apr 2019, 11:50 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

                      1/10

                      24 Apr 2019, 10:08

                      • Login

                      • Login or register to search.
                      1 out of 10
                      • First post
                        1/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved