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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on 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 kshegunovK 2 Replies Last reply
    0
    • sonichyS sonichy

      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 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
      • sonichyS sonichy

        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=" + * + ">");
        }
        
        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #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
        • Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by Paul Colby
          #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.

          sonichyS 2 Replies Last reply
          6
          • Paul ColbyP Paul Colby

            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.

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

            @Paul-Colby

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

            https://github.com/sonichy

            Paul ColbyP 1 Reply Last reply
            0
            • sonichyS sonichy

              @Paul-Colby

              warning: unknown escape sequence: '\ \]'
              QRegularExpression r("!\[([^[]*)\]\\(([^(]*)\\)");
              
              Paul ColbyP Offline
              Paul ColbyP Offline
              Paul Colby
              wrote on last edited by Paul Colby
              #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
              • Paul ColbyP Paul Colby

                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.

                sonichyS Offline
                sonichyS Offline
                sonichy
                wrote on last edited by sonichy
                #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

                • Login

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