Important: Please read the Qt Code of Conduct -

QString: replace first and second appear string

  • Replace first "~~" to "<s>", second "~~" to "</s>".

  • Moderators

    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;

  • @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, 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>");

  • Moderators

    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.

  • @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... :)

  • @JonB

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

  • Lifetime Qt Champion

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

  • 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;"

    something <s>token</s> more

    Sorry if mentioning a different tool is blasphemy. LOL

  • @aha_1980 Thank you !

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

Log in to reply