Farzan Najipour
Farzan Najipour

Reputation: 2493

Capture number inside tag in Qt

My tag struct looks like this:

<sml8/>

combination of < , sml , digits (one or two) and /> Is there anyway to capture number inside tag?
for example in above I want capture 8 inside

I've defined regular expression and I tried to capture it by digit position but it's not working for me.

QRegExp rxlen("<sml(.*)/>");
int index = rxlen.pos(3);

I guess it's not correct way and it gives me position of digit although I want value of digit (or digits).

Upvotes: 1

Views: 149

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627292

You need to use capturedTexts() together with <sml(\\d{1,2})/> regex (it matches <sml literally, then 1 or 2 digits capturing them into Captured group 1, then />:

QString str = "<sml8/>";
QRegExp rxlen("<sml(\\d{1,2})/>");
int pos = rxlen.indexIn(str);
QStringList list = rxlen.capturedTexts();
QString my_number = list[1];

Upvotes: 1

Related Questions