I've worked on regular expression on Qt , I want to replace all sub string with specific regular expression with image.
my tag struct is combination of <
, sml
, digits (one or two)
and />
and my QString is draftMsg
. It will work if I use regular expression once.
for example : "hello <sml7/>
" will be change to hello and photo with label 7 in my directory.
Here is my code:
QRegExp rxlen("<sml(\\d{1,2})/>");
if (draftMsg.contains(rxlen))
{
QString str = rxlen.capturedTexts()[1];
int index = str.toInt();
smileyClicked(index-1);
m_messageEdit->insertHtml(QString("<img src=\":images/smiley/%1_64.png\" width=%2 />")
.arg(index, 2, 10, QLatin1Char('0')).arg(smileyWidth));
draftMsg = draftMsg.remove(rxlen);
}
Actually it replace wrongly when I write string like : "hello <sml7/><sml1/>
". It will replace both tag to image with label 7.
I searched and I found it. I trying to use captureCount() to keep Regular expression's number and use it.
I've created this function:
void MessageDialog::regInMessage(QString pattern, QString string)
{
QRegExp regex(pattern);
if (regex.indexIn(string) < 0) {
qDebug("Can't find a match.");
return;
}
qDebug() << regex.captureCount();
}
But it gives me "1" instead of two.
Any suggestion to count regular expression on my QString?
qDebug() << regex.cap(0); qDebug() << regex.cap(1);qDebug() << regex.cap(2);
??