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. QRegExp replace
Forum Updated to NodeBB v4.3 + New Features

QRegExp replace

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.7k Views 3 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 2 Apr 2018, 09:04 last edited by
    #1

    Suppose * and # stand for 2 any string, how to get and replace [*] and (#) ?

    QString s = "![alt](preview.png)"
    if (s.contains("![*](#)")) {
        s.replace("![*](#)", "<img src=" + # + " alt=" + * + ">");
    }
    

    https://github.com/sonichy

    K K 2 Replies Last reply 2 Apr 2018, 10:15
    0
    • S sonichy
      2 Apr 2018, 09:04

      Suppose * and # stand for 2 any string, how to get and replace [*] and (#) ?

      QString s = "![alt](preview.png)"
      if (s.contains("![*](#)")) {
          s.replace("![*](#)", "<img src=" + # + " alt=" + * + ">");
      }
      
      K Offline
      K Offline
      koahnig
      wrote on 2 Apr 2018, 10:15 last edited by
      #2

      @sonichy

      You might want to check out QRegularExpression which is the newer version for regular expressions. It follows the general, common rules for regular expressions.

      You are using a couple of special characters for regular expressions. This is true for '[', ']', '*', '(' and ')'. You need to check if you can escape those letters with a back slash upfront.

      The best is to use the regular expression example for trials (e.g. for QRegExp ). There you can easily see directly if your regular expression is going to capture what you think.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      6
      • S sonichy
        2 Apr 2018, 09:04

        Suppose * and # stand for 2 any string, how to get and replace [*] and (#) ?

        QString s = "![alt](preview.png)"
        if (s.contains("![*](#)")) {
            s.replace("![*](#)", "<img src=" + # + " alt=" + * + ">");
        }
        
        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 3 Apr 2018, 20:36 last edited by kshegunov 4 Mar 2018, 20:38
        #3

        For QRegularExpression with allowed escaping: !\[((?:(?<=\\)\]|[^\]])+)\]\(((?:(?<=\\)\)|[^\)])+)\)

        PS. Test is here: https://regex101.com/r/mvkzHU/1

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        4
        • P Offline
          P Offline
          Paul Colby
          wrote on 4 Apr 2018, 05:41 last edited by Paul Colby 4 May 2018, 03:52
          #4

          An example for inspiration:

              QString s = "start![alt1](preview1.png)middle![alt2](preview2.png)end";
              qDebug().noquote() << "Before:" << s;
          
              const QRegularExpression r("!\\\[([^[]*)\\\]\\(([^(]*)\\)");
              for (QRegularExpressionMatch m = r.match(s); m.hasMatch(); m = r.match(s)) {
                  s.replace(m.capturedStart(0), m.capturedLength(0),
                            QString("<img src=\"%1\" alt=\"%2\">").arg(m.captured(2)).arg(m.captured(1)));
              }
          
              qDebug().noquote() << "After: " << s;
          

          This outputs:

          Before: start![alt1](preview1.png)middle![alt2](preview2.png)end
          After:  start<img src="preview1.png" alt="alt1">middle<img src="preview2.png" alt="alt2">end
          

          Cheers.

          S 2 Replies Last reply 5 Apr 2018, 03:23
          6
          • P Paul Colby
            4 Apr 2018, 05:41

            An example for inspiration:

                QString s = "start![alt1](preview1.png)middle![alt2](preview2.png)end";
                qDebug().noquote() << "Before:" << s;
            
                const QRegularExpression r("!\\\[([^[]*)\\\]\\(([^(]*)\\)");
                for (QRegularExpressionMatch m = r.match(s); m.hasMatch(); m = r.match(s)) {
                    s.replace(m.capturedStart(0), m.capturedLength(0),
                              QString("<img src=\"%1\" alt=\"%2\">").arg(m.captured(2)).arg(m.captured(1)));
                }
            
                qDebug().noquote() << "After: " << s;
            

            This outputs:

            Before: start![alt1](preview1.png)middle![alt2](preview2.png)end
            After:  start<img src="preview1.png" alt="alt1">middle<img src="preview2.png" alt="alt2">end
            

            Cheers.

            S Offline
            S Offline
            sonichy
            wrote on 5 Apr 2018, 03:23 last edited by sonichy 4 May 2018, 03:25
            #5

            @Paul-Colby

            warning: unknown escape sequence: '\ \]'
            QRegularExpression r("!\[([^[]*)\]\\(([^(]*)\\)");
            

            https://github.com/sonichy

            P 1 Reply Last reply 5 Apr 2018, 03:55
            0
            • S sonichy
              5 Apr 2018, 03:23

              @Paul-Colby

              warning: unknown escape sequence: '\ \]'
              QRegularExpression r("!\[([^[]*)\]\\(([^(]*)\\)");
              
              P Offline
              P Offline
              Paul Colby
              wrote on 5 Apr 2018, 03:55 last edited by Paul Colby 4 May 2018, 03:55
              #6

              Hi @sonichy

              Sorry, the forum software is buggy (actually renders properly in the preview panel, yet breaks the presentation when posted). I've updated the post above so it displays correctly.

              The filter should look like: "!\\\[([^[]*)\\\]\\(([^(]*)\\)"

              Cheers.

              1 Reply Last reply
              2
              • P Paul Colby
                4 Apr 2018, 05:41

                An example for inspiration:

                    QString s = "start![alt1](preview1.png)middle![alt2](preview2.png)end";
                    qDebug().noquote() << "Before:" << s;
                
                    const QRegularExpression r("!\\\[([^[]*)\\\]\\(([^(]*)\\)");
                    for (QRegularExpressionMatch m = r.match(s); m.hasMatch(); m = r.match(s)) {
                        s.replace(m.capturedStart(0), m.capturedLength(0),
                                  QString("<img src=\"%1\" alt=\"%2\">").arg(m.captured(2)).arg(m.captured(1)));
                    }
                
                    qDebug().noquote() << "After: " << s;
                

                This outputs:

                Before: start![alt1](preview1.png)middle![alt2](preview2.png)end
                After:  start<img src="preview1.png" alt="alt1">middle<img src="preview2.png" alt="alt2">end
                

                Cheers.

                S Offline
                S Offline
                sonichy
                wrote on 5 Apr 2018, 13:53 last edited by sonichy 4 May 2018, 15:06
                #7

                @Paul-Colby Work fine !
                Should we use

                if(s.contains("![*](#)"))
                

                first, because there is

                if(s.contains("[*](#)"))
                

                to deal with.

                https://github.com/sonichy

                1 Reply Last reply
                0

                1/7

                2 Apr 2018, 09:04

                • Login

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