#include <QRegExp>
//识别样式
QRegExp r("(\\S+)\\s*[\\(<]((http|https|ftp)://.*)[\\)>]");
int pos = r.indexIn(htmltext);
if (pos > -1) { //pos > -1 代表已经匹配到相关字串
QString sourcestring = r.cap(0); //r.cap(0)返回第一个匹配到的字符串。cap()参数缺省值为0;
QString textLabel = r.cap(1); // 匹配字串第一个() 的内容 即:(\\S+)
QString textURL = r.cap(2); //匹配字串第二个()的内容,即:((http|https|ftp)://.*)[\\)
QString deststring = "<a href=\"" + textURL + "\">" + textLabel + "<\\a>";
htmltext.replace(sourcestring, deststring);// 将匹配到的r.cap(0) 转换成有a标签的内容deststring
}
QRegexp
最新推荐文章于 2024-03-11 15:34:39 发布