Capturing a number inside a tag in Qt
My tag structure looks like this:
<sml8/>
combination of <
, sml
, digits (one or two)
and />
is there a way to capture the number in the tag? For example, for example above I want to capture 8 inside
I have defined a regex and I tried to lock it by the position of the digits, but it doesn't work for me.
QRegExp rxlen("<sml(.*)/>");
int index = rxlen.pos(3);
I assume this is not the correct way and it gives me the position of the digit, although I want the value of the digit (or digits).
source to share
You need to use captureTexts () along 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];
source to share