Reputation: 19
I'm struggling to get a regular expression implemented. I'm using Qt creator on an Ubuntu system. I tested my regex against an example number with a 3rd party tool. So I believe the problem is not with the expression. My desired reg ex:
/\b(9410 ?\d{18})\b/i
I am putting the regex string into a QString variable. Which results in an error:
QString test = "/\b(9410 ?\d{18})\b/i"; unknown escape sequence '\d'
In an attempt to fix, I add an extra \ at the point of the error:
QString test = "/\b(9410 ?\\d{18})\b/i"; qWarning() << test;
Debugger indicates (note the \\):
/\b(9410 ?\\d{18})\b/i
I also tried a raw string:
QString test = R"(/\b(9410 ?\d{18})\b/i)"; qWarning() << test;
Debugger shows all single \ replaced with \\.
/\\b(9410 ?\\d{18})\\b/i
None of these attempts has resulted in a working reg ex. There is something fishy going on with the back slashes. Appreciate your thoughts. I must be missing something simple...
EDIT: Here is some simplified code. When I run this it returns "FALSE" indicating no match. I tested this regex and number at regex101.com. Works there. That's why I believe something is flawed in my implementation. Just can't put my finger on it.
QRegularExpression re;
QString test = R"(/\b(9410 ?\d{18})\b/i)";
re.setPattern(test);
if(re.match("9410811298370146293071").hasMatch())
{
qWarning() << "TRUE";
}
else {
qWarning() << "FALSE";
}
Upvotes: 0
Views: 1041
Reputation: 19
Cleaned up the regex and it now matches.
QRegularExpression re;
QString test = R"(9410 ?\d{18})";
re.setPattern(test);
if(re.match("9410811298370146293071").hasMatch())
{
qWarning() << "TRUE";
}
else {
qWarning() << "FALSE";
}
Upvotes: 1