Regex return nothing with QRegularExpression
-
hi everyone,
I have created a desktop application with QT 5.13.2 and MSVC 2017 32 bit
It's an electronic stock manager with mongoDB in backgroundI'm doing the search engine for my app.
I've a search Field, where i can use some basic syntax.eg:
Search with value only:
- value
- value,value,value;
- value|value|value;
Search with referenced value
- ref=value
- ref=value&ref=value&ref=value;
- ref=value|ref=value|ref=value;
First i planned to implement a parser, but i thinks it's a bit oversized for this purpose. So i'm gonna using regex, but i never doing that before.
I have doing on regex101.com my regex that seem do be fonctional on the site, but when i run it with QRegularExpression they don't return the same thing.
They are in PCRE .
reference value = ([\w]{1,}[\s]{0,})([\=|\<|\>])([\s]{0,}[\w]{1,})([\s]{0,}[\&|\||\<|\>\;]) value only = ([\w]{1,}[\,\h\;\|\|]{1})
actually for the two regex, if i input :
value
OR
ref=value
I need to add an semi colon a the end of text. how i could rig of that ?And if the string is :
element=value&element=value&element=value;and in debugger, i watch the QStringList the only thing that comes in is: "element"
could you please help me for this regex problem ?
My c++ code is: (I'm testing only referenced value here)
const QString regexByRef = "([\\w]{1,}[\\s]{0,})([\\=|\\<|\\>])([\\s]{0,}[\\w]{1,})([\\s]{0,}[\\&|\\||\\<|\\>\\;])"; const QString regexByValue = "([\\w]{1,}[\\,\\h\\;\\|\\&]{1})"; QRegularExpression re(regexByRef,QRegularExpression::CaseInsensitiveOption); re.setPatternOptions(QRegularExpression::MultilineOption); if(re.isValid()) { QRegularExpressionMatchIterator it = re.globalMatch(input); QStringList words; while (it.hasNext()) { QRegularExpressionMatch match = it.next(); QString word = match.captured(1); words << word; } }
-
const QRegularExpression re(QStringLiteral(R"***((?:[\,\;\h\|]?(\w+))|(?:([\&\|\<\>]?)(\w+)\s*=\s*(\w+)\s*\;?))***")); QRegularExpressionMatchIterator it = re.globalMatch(input); while (it.hasNext()) { const QRegularExpressionMatch match = it.next(); switch(match.lastCapturedIndex()){ case 1: //value only qDebug() << "Captured Value: " << match.capturedRef(1); break; case 2: // first referenced value qDebug() << match.capturedRef(1) << "=" << match.capturedRef(2); break; case 3: //subsequent referenced value qDebug() << "Separator: " << match.capturedRef(1); qDebug() << match.capturedRef(2) << "=" << match.capturedRef(3); break; default: Q_UNREACHABLE(); break; } }
-
Hi @VRonin
Thanks for your response,i've tried your code.
if i input:element=value&element=value&element=value;
it returns me :
element value
but i need the separator everytime, plus it gave me only the first occurence of
element=value
after that it raise the exception Q_UNREACHABLE
-
ok, this is tested and should be working:
#include <QRegularExpression> #include <QDebug> int main(int argc, char *argv[]) { const QString inputs[] = { QStringLiteral("value") ,QStringLiteral("value,value,value;") ,QStringLiteral("value|value|value;") ,QStringLiteral("ref=value") ,QStringLiteral("ref=value&ref=value&ref=value;") ,QStringLiteral("ref=value|ref=value|ref=value;") ,QStringLiteral("element=value&element=value&element=value;") }; const QRegularExpression re(QStringLiteral(R"***(([\&\|\<\>\h,]?)(\w+)\s*(?:=\s*(\w+)\s*\;?)?)***")); for(const QString& input : inputs){ qDebug() << "Input: " << input; QRegularExpressionMatchIterator it = re.globalMatch(input); while (it.hasNext()) { const QRegularExpressionMatch match = it.next(); switch(match.lastCapturedIndex()){ case 2: qDebug() << "Value Only"; if(!match.capturedRef(1).isEmpty()) qDebug() << "Previous separator: " << match.capturedRef(1); qDebug() << "Value: " << match.capturedRef(2); break; case 3: qDebug() << "Referenced Value"; if(!match.capturedRef(1).isEmpty()) qDebug() << "Previous separator: " << match.capturedRef(1); qDebug() << "Value: " << match.capturedRef(2) << "=" << match.capturedRef(3); break; default: Q_UNREACHABLE(); break; } } } return 0; }