A question about regex usage using Qt library
-
wrote on 18 Dec 2022, 11:18 last edited by HFT_developer
Hi,
I have managed to create a regex that can be used to verify polynomials. My algorithm to verify the polynomial is that if the number of matches and the size of the length of the polynomial are the same that poly must be valid.
Using C++ STL, I could do something like:
std::string sample1 = {"4x^3 - 9x^2 + 10x^1 -5x^0 -45x^-1"}; // valid poly std::string sample2 = {"4x^3 - 9x^2 + 10x -5 -45x^-1"};// invalid poly // I removed all the white space from the input string bool verifyPolynomial(const std::string& poly){ std::smatch matches; std::regex _regex("(-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+");// this is tested on regex101 website std::regex_search(poly, matches, _regex); return (matches.size() == poly.size() ); }
Link: https://regex101.com/r/D7HjR7/1
How do I convert this into Qt equivalent code ?
I have something like this so far:
QRegularExpression Gui_app_handler::m_regex2 = QRegularExpression("(-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+"); // this function crashes the app bool Gui_app_handler::verifyPolynomial(const QString &polynomial){ int _matches{}; QRegularExpressionMatchIterator it = m_regex2.globalMatch(polynomial); while (it.hasNext() ){ ++_matches; } return (_matches == polynomial.size()); }
-
wrote on 18 Dec 2022, 11:34 last edited by
Your
while
loop never advances the iterator, by callingnext()
, so "crashes the app" is probably an endless, CPU intensive loop. -
Your
while
loop never advances the iterator, by callingnext()
, so "crashes the app" is probably an endless, CPU intensive loop.wrote on 18 Dec 2022, 11:55 last edited by@ChrisW67 ok thanks. Actually how can I convert the regex101 to C++ regex, because this gives warning:
// this one gives warning from: https://regex101.com/r/D7HjR7/1 QRegularExpression Gui_app_handler::m_regex2 = QRegularExpression("(-|\+)?[0-9]+[a-z]\^(-|\+)?[0-9]+"); QRegularExpression Gui_app_handler::m_regex2 = QRegularExpression("(-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+");// I removed the warning but this is not matching as expected as regex 101 bool Gui_app_handler::verifyPolynomial(const QString &polynomial){ int _matches{}; QRegularExpressionMatchIterator it = m_regex2.globalMatch(polynomial); while (it.hasNext() ){ it.next(); ++_matches; } return (_matches == polynomial.size()); }
-
@ChrisW67 ok thanks. Actually how can I convert the regex101 to C++ regex, because this gives warning:
// this one gives warning from: https://regex101.com/r/D7HjR7/1 QRegularExpression Gui_app_handler::m_regex2 = QRegularExpression("(-|\+)?[0-9]+[a-z]\^(-|\+)?[0-9]+"); QRegularExpression Gui_app_handler::m_regex2 = QRegularExpression("(-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+");// I removed the warning but this is not matching as expected as regex 101 bool Gui_app_handler::verifyPolynomial(const QString &polynomial){ int _matches{}; QRegularExpressionMatchIterator it = m_regex2.globalMatch(polynomial); while (it.hasNext() ){ it.next(); ++_matches; } return (_matches == polynomial.size()); }
wrote on 18 Dec 2022, 14:01 last edited by JonB@HFT_developer said in A question about regex usage using Qt library:
because this gives warning:
What warning? A warning whose text you don't show but you'd like people to guess and fix?
// I removed the warning but this is not matching as expected as regex 101
And what is it doing?
-
@HFT_developer said in A question about regex usage using Qt library:
because this gives warning:
What warning? A warning whose text you don't show but you'd like people to guess and fix?
// I removed the warning but this is not matching as expected as regex 101
And what is it doing?
wrote on 18 Dec 2022, 17:25 last edited by@JonB The warning was about unknown escape sequence which is
\+
but should be\\+
. So I fixed it:QRegularExpression Gui_app_handler::m_regex2 = QRegularExpression("((-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+)+");
So using STL I've got it to work:
bool verifyPolynomial(const std::string& poly){ std::regex _regex("((-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+)+"); return regex_match(poly, _regex); } int main() { std::string sample1 = {"4x^3-9x^2+10x^1-5x^0-45x^-1"}; // should be valid std::string sample2 = {"4x^3-9x^2+10x^1-5x^0-45x^"};// should be invalid std::cout<<"verifyPolynomial1: "<<verifyPolynomial(sample1)<<"\n"; std::cout<<"verifyPolynomial2: "<<verifyPolynomial(sample2)<<"\n"; }
For Qt I did this:
bool Gui_app_handler::verifyPolynomial(const QString &polynomial){ QRegularExpressionMatch match = m_regex2.match(polynomial); return match.hasMatch(); } // but this function is returning true and for wrong stuff like "4x^3-9x^2+10x^1-5x^0-45x^"
Why is the Qt one not functioning the same as STL?
-
Hi,
As silly as it may sound: because Qt != STL.
Note that Qt had regular expressions classes way before std did. So the question could be asked the other way around, why does std not behave like Qt ?
Anyway, the goal here is not to start a flamewar. If you take the time to do some spelunking with regard to Qt and the STL, you will see that many things you find now in the std have origins in Qt.
There will always be differences though but both can coexist.
-
@JonB The warning was about unknown escape sequence which is
\+
but should be\\+
. So I fixed it:QRegularExpression Gui_app_handler::m_regex2 = QRegularExpression("((-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+)+");
So using STL I've got it to work:
bool verifyPolynomial(const std::string& poly){ std::regex _regex("((-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+)+"); return regex_match(poly, _regex); } int main() { std::string sample1 = {"4x^3-9x^2+10x^1-5x^0-45x^-1"}; // should be valid std::string sample2 = {"4x^3-9x^2+10x^1-5x^0-45x^"};// should be invalid std::cout<<"verifyPolynomial1: "<<verifyPolynomial(sample1)<<"\n"; std::cout<<"verifyPolynomial2: "<<verifyPolynomial(sample2)<<"\n"; }
For Qt I did this:
bool Gui_app_handler::verifyPolynomial(const QString &polynomial){ QRegularExpressionMatch match = m_regex2.match(polynomial); return match.hasMatch(); } // but this function is returning true and for wrong stuff like "4x^3-9x^2+10x^1-5x^0-45x^"
Why is the Qt one not functioning the same as STL?
wrote on 18 Dec 2022, 22:27 last edited by@HFT_developer said in A question about regex usage using Qt library:
Why is the Qt one not functioning the same as STL?
The secret is in the manual.
STL std::regex_match() attempts to match a regular expression to an entire string
Qt QRegularExpression::match() matches the pattern anywhere in the string (after the offset if provided).
4x^3-9x^2+10x^1-5x^0-45x
is matched because the RE matches4x^3-9x^2+10x^1-5x^0
QRegularExpression will honour the ^ and/or $ anchors if present, so you can use those to match on a whole string. -
wrote on 18 Dec 2022, 22:44 last edited by
you may be able to save some time if you use equation passer libs.
https://beltoforion.de/en/muparser/
1/8