Extract Number from an Alphanumeric QString
-
wrote on 26 Jul 2017, 04:34 last edited by
I have a QString of "s150 d300". How can I get the numbers from the QString and convert it into integer. Simply using 'toInt' is not working.
Let say, from the QString of "s150 d300", only the number after the alphabet 'd' is meaningful to me. How can I extract the value of '300' from the string?
Thank you very much for your time.
-
I have a QString of "s150 d300". How can I get the numbers from the QString and convert it into integer. Simply using 'toInt' is not working.
Let say, from the QString of "s150 d300", only the number after the alphabet 'd' is meaningful to me. How can I extract the value of '300' from the string?
Thank you very much for your time.
QRegularExpression rx("[0-9]+"); QRegularExpressionMatch match = rx.match("s150 d300"); if ( match.hasMatch() ) { QString matched1 = match.captured(0); // matched == "150" QString matched2 = match.captured(1); // matched == "300" }
if you really just want the number after the "d" only use this:
QRegularExpression rx("d([0-9]+)"); QRegularExpressionMatch match = rx.match("s150 d300"); if ( match.hasMatch() ) { QString matched1 = match.captured(0); // matched == "300" }
(untested)
-
I have a QString of "s150 d300". How can I get the numbers from the QString and convert it into integer. Simply using 'toInt' is not working.
Let say, from the QString of "s150 d300", only the number after the alphabet 'd' is meaningful to me. How can I extract the value of '300' from the string?
Thank you very much for your time.
wrote on 26 Jul 2017, 07:06 last edited by m.sueHi @Shahmisufi
QString szStr="s150 d300"; QRegExp regExp(qa(".*d(\\d*)"),Qt::CaseSensitive,QRegExp::RegExp); //non-greedy wildcards if (regExp.indexIn(szStr) > 0) { QString szNumber=regExp.cap(1); //do something }
-Michael
-
Hi @Shahmisufi
QString szStr="s150 d300"; QRegExp regExp(qa(".*d(\\d*)"),Qt::CaseSensitive,QRegExp::RegExp); //non-greedy wildcards if (regExp.indexIn(szStr) > 0) { QString szNumber=regExp.cap(1); //do something }
-Michael
@m.sue
just to note:
QRegExp
is deprecated since Qt5.QRegularExpression
should be used instead. -
QRegularExpression rx("[0-9]+"); QRegularExpressionMatch match = rx.match("s150 d300"); if ( match.hasMatch() ) { QString matched1 = match.captured(0); // matched == "150" QString matched2 = match.captured(1); // matched == "300" }
if you really just want the number after the "d" only use this:
QRegularExpression rx("d([0-9]+)"); QRegularExpressionMatch match = rx.match("s150 d300"); if ( match.hasMatch() ) { QString matched1 = match.captured(0); // matched == "300" }
(untested)
wrote on 26 Jul 2017, 07:14 last edited by\\\\d
why the 4(i.e. 2) slashes? shouldn't it just be
"d(\\d+)"
?Anticipating the questions:
\d
is the same as[0-9]
-
\\\\d
why the 4(i.e. 2) slashes? shouldn't it just be
"d(\\d+)"
?Anticipating the questions:
\d
is the same as[0-9]
@VRonin
I wanted to make clear that\d
isn't related to the "d" in the string so i used[0-9]
.
You are right, in the second example i was thinking too complicated. Updated my post. -
wrote on 27 Jul 2017, 12:10 last edited by
You given string example "s150 d300" and in real case string can be anything. According to me your requirement is last occurrence number you required from input string. Below added works for all scenario's.
pass any string to below function , output will be last occurance number
this->lastoccurancenumber("s150d300");
void lastoccurancenumber(QString str)
{int final_number; QString s = str; QString s1=""; for(int i = s.length()-1;i>=0;i--) { QString temp = s[i]; QByteArray ba = temp.toLatin1(); char *c_str2 = ba.data(); char c_temp = c_str2[0]; if(!isalpha(c_temp)) { qDebug()<<"string"<<s[i]<<"nu,ber is"<<temp<<endl; s1.append(temp); qDebug()<<"charater is"<<s1<<endl; } else { break; } } std::reverse(s1.begin(), s1.end()); final_number = s1.toInt(); qDebug()<<"charater is"<<final_number<<endl;
}
-
You given string example "s150 d300" and in real case string can be anything. According to me your requirement is last occurrence number you required from input string. Below added works for all scenario's.
pass any string to below function , output will be last occurance number
this->lastoccurancenumber("s150d300");
void lastoccurancenumber(QString str)
{int final_number; QString s = str; QString s1=""; for(int i = s.length()-1;i>=0;i--) { QString temp = s[i]; QByteArray ba = temp.toLatin1(); char *c_str2 = ba.data(); char c_temp = c_str2[0]; if(!isalpha(c_temp)) { qDebug()<<"string"<<s[i]<<"nu,ber is"<<temp<<endl; s1.append(temp); qDebug()<<"charater is"<<s1<<endl; } else { break; } } std::reverse(s1.begin(), s1.end()); final_number = s1.toInt(); qDebug()<<"charater is"<<final_number<<endl;
}
@Seenu
Motto: why easy when u can do it complicated -
QRegularExpression rx("[0-9]+"); QRegularExpressionMatch match = rx.match("s150 d300"); if ( match.hasMatch() ) { QString matched1 = match.captured(0); // matched == "150" QString matched2 = match.captured(1); // matched == "300" }
if you really just want the number after the "d" only use this:
QRegularExpression rx("d([0-9]+)"); QRegularExpressionMatch match = rx.match("s150 d300"); if ( match.hasMatch() ) { QString matched1 = match.captured(0); // matched == "300" }
(untested)
wrote on 27 Jul 2017, 12:52 last edited by@raven-worx I have tested the code and it returns "d300" instead of "300"
-
wrote on 28 Jul 2017, 03:22 last edited by
Try the code once which i posted ....
-
@raven-worx I have tested the code and it returns "d300" instead of "300"
@Shahmisufi This works for me:
QRegularExpression rx("[0-9]+"); QRegularExpressionMatchIterator matches = rx.globalMatch("s150 d300"); while (matches.hasNext()) { QRegularExpressionMatch match = matches.next(); qDebug() << match.captured(0); }
@Seenu your solution might work (I didn't test it) but it is much more complex compared to a solution using a simple regular expression.
-
Ya its complex , but it will return last occurrence number in a string and is in generic format
@Seenu What do you mean by "generic format"?
You can do the same with a simple regular expression.
1/13