QRegExp replace
-
wrote on 2 Apr 2018, 09:04 last edited by
Suppose * and # stand for 2 any string, how to get and replace [*] and (#) ?
QString s = "" if (s.contains("")) { s.replace("", "<img src=" + # + " alt=" + * + ">"); }
-
Suppose * and # stand for 2 any string, how to get and replace [*] and (#) ?
QString s = "" if (s.contains("")) { s.replace("", "<img src=" + # + " alt=" + * + ">"); }
wrote on 2 Apr 2018, 10:15 last edited byYou 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.
-
Suppose * and # stand for 2 any string, how to get and replace [*] and (#) ?
QString s = "" if (s.contains("")) { s.replace("", "<img src=" + # + " alt=" + * + ">"); }
Moderatorswrote on 3 Apr 2018, 20:36 last edited by kshegunov 4 Mar 2018, 20:38For QRegularExpression with allowed escaping:
!\[((?:(?<=\\)\]|[^\]])+)\]\(((?:(?<=\\)\)|[^\)])+)\)
PS. Test is here: https://regex101.com/r/mvkzHU/1
-
wrote on 4 Apr 2018, 05:41 last edited by Paul Colby 4 May 2018, 03:52
An example for inspiration:
QString s = "startmiddleend"; 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: startmiddleend After: start<img src="preview1.png" alt="alt1">middle<img src="preview2.png" alt="alt2">end
Cheers.
-
An example for inspiration:
QString s = "startmiddleend"; 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: startmiddleend After: start<img src="preview1.png" alt="alt1">middle<img src="preview2.png" alt="alt2">end
Cheers.
wrote on 5 Apr 2018, 03:23 last edited by sonichy 4 May 2018, 03:25warning: unknown escape sequence: '\ \]' QRegularExpression r("!\[([^[]*)\]\\(([^(]*)\\)");
-
warning: unknown escape sequence: '\ \]' QRegularExpression r("!\[([^[]*)\]\\(([^(]*)\\)");
wrote on 5 Apr 2018, 03:55 last edited by Paul Colby 4 May 2018, 03:55Hi @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.
-
An example for inspiration:
QString s = "startmiddleend"; 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: startmiddleend After: start<img src="preview1.png" alt="alt1">middle<img src="preview2.png" alt="alt2">end
Cheers.
wrote on 5 Apr 2018, 13:53 last edited by sonichy 4 May 2018, 15:06@Paul-Colby Work fine !
Should we useif(s.contains(""))
first, because there is
if(s.contains("[*](#)"))
to deal with.
1/7