regex with Qt - indexIn(const QString &) does not work as expected - c++

I am using QRegExp and tries to find whether a QString is containing some pattern. There is no compiling error, but no match is identified at runtime where identification should normally happen. I tested the regexp in Python shell and match occurs with Python. i checked upon Qt doc that syntax is the same for the ergexp I am using. Here is code sample
bool Thing::isConstraint(const QString &cstr_)
{
QRegExp lB1("^(\d+\.?\d*|\d*\.\d+)<=PARAM(\d+)$");
QRegExp lB2("^PARAM(\d+)>=(\d+\.?\d*|\d*\.\d+)$");
QRegExp lB3("^PARAM(\d+)>(\d+\.?\d*|\d*\.\d+)$");
QRegExp lB4("^(\d+\.?\d*|\d*\.\d+)<PARAM(\d+)$");
QRegExp uB5("^(\d+\.?\d*|\d*\.\d+)>=PARAM(\d+)$");
QRegExp uB6("^(\d+\.?\d*|\d*\.\d+)>PARAM(\d+)$");
QRegExp uB7("^PARAM(\d+)<=(\d+\.?\d*|\d*\.\d+)$");
QRegExp uB8("^PARAM(\d+)<(\d+\.?\d*|\d*\.\d+)$");
QRegExp luB9("^(\d+\.?\d*|\d*\.\d+)>=PARAM(\d+)>=(\d+\.?\d*|\d*\.\d+)$");
QRegExp luB10("^(\d+\.?\d*|\d*\.\d+)>PARAM(\d+)>=(\d+\.?\d*|\d*\.\d+)$");
QRegExp luB11("^(\d+\.?\d*|\d*\.\d+)>=PARAM(\d+)>(\d+\.?\d*|\d*\.\d+)$");
QRegExp luB12("^(\d+\.?\d*|\d*\.\d+)>PARAM(\d+)>(\\d+\.?\d*|\d*\.\d+)$");
QRegExp luB13("^(\d+\.?\d*|\d*\.\d+)<=PARAM(\d+)<=(\d+\.?\d*|\d*\.\d+)$");
QRegExp luB14("^(\d+\.?\d*|\d*\.\d+)<=PARAM(\d+)<(\d+\.?\d*|\d*\.\d+)$");
QRegExp luB15("^(\d+\.?\d*|\d*\.\d+)<PARAM(\d+)<=(\d+\.?\d*|\d*\.\d+)$");
QRegExp luB16("^(\d+\.?\d*|\d*\.\d+)<PARAM(\d+)<(\d+\.?\d*|\d*\.\d+)$");
int pos_=0;
if((pos_ = lB1.indexIn(cstr_)) != -1)
{
m_func->setLowerBound((lB1.cap(2)).toInt(),(lB1.cap(1)).toDouble());
return true;
}
else if((pos_ = lB2.indexIn(cstr_)) != -1)
{
m_func->setLowerBound((lB2.cap(1)).toInt(),(lB2.cap(2)).toDouble());
return true;
}
/*
...
*/
return false;
}
This method is called in this other method:
void Thing::setConstraints(QStringList &constraints_)
{
if(!m_func)
return;
for(int j=0;j<constraints_.size();j++)
{
if(isConstraint(constraints_.at(j)))
{
constraints_.removeAt(j);
}
}
m_func->setConstraints(constraints_);
}
In VS2010 Watch, error for lB1.indexIn(cstr_) is: Error: argument list does not match a function .
Second, I would like the isConstraint() method to begin with this check and replace for whitespaces:
QRegExp wsp ("\s+");
cstr_.replace(wsp,"");
how to proceed avoiding const_cast ??
Thanks and regards.
edit ---------
needed to double backslash in C++ - different from Python. Tks!

I think you asked two questions, so I'll try to answer them:
1) Your regular expressions are most likely not passing because you need to escape your backslashes so that C++ doesn't mess up your strings. For example:
QRegExp lB1("^(\\d+\\.?\\d*|\\d*\\.\\d+)<=PARAM(\\d+)$");
2) To avoid using const_cast you can either change your function signature to this:
bool Thing::isConstraint( QString cstr_)
or make a copy of the cstr_ object and operate on the copy instead.
As a side note, you may want to take a look at the QRegExp::exactMatch() function which obviates the need to use ^ and $ at the beginning and end of all of your expressions, and also has a bool return value which would make your if statements a little cleaner.

Related

NAPI: How to match a JS regex from a C++ thread?

I am modifying a Node native extension that is spawning native threads to do some processing. My issue is that I'd like to have the Javascript code provide a filter for the processing to exclude some data.
At this point, I'm passing a JS RegExp string from JS to C++, creating a std::regex instance from it, and passing it around the different structures down to the native thread logic.
My issue now is that despite std::regex using what seems to be the same syntax as ECMAScript regular expressions, the behavior is not the same :(
My original plan was to rely on V8's RegExp engine somehow but trigger the C++ bits directly instead of going from C++ to JS and back. But I wasn't able to find how to do this.
As example, see the following programs using the same regex but yielding different results:
#include <stdio.h>
#include <regex>
int main() {
std::regex re("^(?:(?:(?!(?:\\/|^)\\.).)*?\\/c)$");
std::smatch match;
std::string input("a.b/c");
int result = std::regex_match(input, match, re);
if (result == 1) {
printf("ok");
} else {
printf("nok");
}
return 0;
}
The equivalent JS code:
const re = new RegExp("^(?:(?:(?!(?:\\/|^)\\.).)*?\\/c)$");
const match = re.exec("a.b/c");
if (match) {
console.log("ok");
} else {
console.log("nok");
}
My question then is: What can I do to get the same results I would in JS but in C++? Is it possible to run V8's RegExp from a pure C++ context?

Diacritical marks in regular expression causes unexpected behavior

I check name validity by this regular expression, allowing any symbol as suggested here:
// Allow any symbol
const QString validNameMatcher = QStringLiteral("^[a-zA-Z0-9 _.,!()+=`,\"#$#%*-]+$");
bool Class::isNameValid(const QString fileName)
{
QRegularExpression re(validNameMatcher);
QRegularExpressionMatch match = re.match(fileName);
if (match.hasMatch())
return true;
else
return false;
}
For a file name like 1111 Rick (wow) L50-57.stl the above function returns true. So far so good.
To allow diacritical marks, I just add [À-ž] to the name-matcher as suggested here:
// [À-ž] is for diacritical marks
const QString validNameMatcher = QStringLiteral("^[a-zA-Z0-9À-ž _.,!()+=`,\"#$#%*-]+$");
After adding [À-ž], surprisingly, for the same file name of 1111 Rick (wow) L50-57.stl, the above function returns false. Am I missing something?
UPDATE
As suggested by #WiktorStribiżew , I used UseUnicodePropertiesOption:
QRegularExpression re(validNameMatcher, QRegularExpression::PatternOption::UseUnicodePropertiesOption);
But it didn't work. The result is the same as before.
Also (*UTF) doesn't work:
const QString validNameMatcher = QStringLiteral("(*UTF)^[a-zA-Z0-9À-ž _.,!()+=`,\"#$#%*-]+$");
The key point is #WiktorStribiżew solution of using QRegularExpression::UseUnicodePropertiesOption option:
QRegularExpression re(validNameMatcher, QRegularExpression::PatternOption::UseUnicodePropertiesOption);
But as mentioned on its documentation:
QRegularExpression::UseUnicodePropertiesOption
The meaning of the \w, \d, etc., character classes, as well as the meaning of their counterparts (\W, \D, etc.), is changed from matching ASCII characters only to matching any character with the corresponding Unicode property.
So, it occurred to me to replace [a-zA-Z0-9À-ž_] in my regular expression with just [\w]:
// Bad:
const QString validNameMatcher = QStringLiteral("^[a-zA-Z0-9À-ž _.,!()+=`,\"#$#%*-]+$");
// Good:
const QString validNameMatcher = QStringLiteral("^[\\w .,!()+=`,\"#$#%*-]+$");
Now, isNameValid() function returns expected results.

(Qt) Validate string against multiple regular expressions simultaneously

I'm checking a string which contains vehicle registration information against regular expressions for validity. I have several regular expression for each criteria I need. How can I validate the string against all my reg expressions without having to combine them into one expression or do something like this to determine if it's valid?
if( s_expGP.exactMatch(lineEdit->text()) ||
s_expGPNew.exactMatch(lineEdit->text()) ||
s_expPersonal.exactMatch(lineEdit->text()) ||
s_expGov.exactMatch(lineEdit->text()) )
{
//do stuff
}
The only option would be to create a single regular expression by combining s_expGP, s_expGPNew, s_expPersonal and the rest if that is possible, otherwise I don't think there could be any other way.
If you have a big number of regexp to test or if you may need to verify the string more than once. You can create a function like this
bool isValid(const QVector<QRegExp>& regExps, const QString& input)
{
for(QRegExp exp : regExps)
{
if(!exp.exactMatch(input))
return false;
}
return true;
}
Or use a static QVector like you have static regexp.

How to check a specified string is a valid URL or not using C++ code

there any possible way to check that the specified string is a valid url or not. The solution must be in c++ and it should work without internet.
example strings are
good.morning
foo.goo.koo
https://hhhh
hdajdklbcbdhd
8881424.www.hfbn55.co.in/sdfsnhjk
://dgdh24.vom
dfgdfgdf(2001)/.com/sdgsgh
\adiihsdfghnhg.co.inskdhhj
aser//www.gtyuh.co.uk/kdsfgdfgfrgj
Chose a symphatetic regular expression like /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.
Use std regex, or boost regex if you don't have C++11:
if (std::regex_match ("http://subject", std::regex("^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$") )) {
// ...
}
You could use regex.
What a regex is.
With C++11 the regex are build-in the STD library
regex c++11.
If you cannot use C++11, for some reason, you could use boost library.
Anyway you could check the patter of an url with:
#include <regex> //require c++11
// ...
// regex pattern
std::string pattern = "https?:\/\/(www\.)?[-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9#:%_\+.~#?&//=]*)";
// Construct regex object
std::regex url_regex(pattern);
// An url-string for example
std::string my_url = "http://www.google.com/img.png";
// Check for match
if (std::regex_match(my_url, url_regex) == true) {
std::cout << "This is a well-formed url\n";
} else {
std::cout << "Ill-formed url\n";
}

not able to return QString from c++ function to PERL function by using SWIG

I have c++ code (Includes Qt also), i want to call those functions by using Perl.
Which we can do by using SWIG, so I have implemented interfaces and did all the stuff need to use them in Perl script.
I have a function in c++ which returns a QString value,
QString get_string()
{
return QString("mystring");
}
I have written one more class which will be used in perl script where i have a function which calls this get_string() function and returns const char*.
const char* get_const_string()
{
QString str = get_string();
**//here I print str and str .toLocal8Bit().constData()
//both are printing the text which i shoud get here**
return str.toLocal8Bit().constData();
//here I have tried diff combinations also, as
// return str.toStdString().c_str();
}
The problem is, in get_const_string() function, I could get the string I wanted, but when I call this function in my perl script, I am getting undefine value i.e null string
.
Any idea, what is the problem here ??
I am using perl5, Qt4.8.4
Thanks in advance.
if you cant use a QString return value, maybe you can use std::string.
if both fail and you do not have limitations, you could do some dirty trick:
QString get_string()
{
static QByteArray arr;
QString str = getString();
arr = str.toLocal8Bit();
return arr.constData();
}
note that the arr variable will not be free'd untill your app is running
edit: found a possible solution to just use std::string ... string arguments are not recognized by SWIG