PDA

View Full Version : QRegExp for extracting the string from log



quantiti
12th December 2016, 05:34
Please help, I'm new in QT in I've tried some regex but it not works as I expected.
I have some log string like this:
"(indicators1)somethings&para1=value1&para2=value2&something"

I want to extract to a QListString these values:
indicators1
para1
value1
para2
value2

value1, value2 can contain any charactor
Could anyone please help?

anda_skoa
12th December 2016, 11:18
value1, value2 can contain any charactor

Any character? Even "&"?

Cheers,
_

quantiti
12th December 2016, 17:18
Thank you admin.
Value has been encoded by url encode. such as @ -> %40, & ->%25
So value contains a-zA-Z.%

quantiti
13th December 2016, 05:38
could anyone help, please

MatrixSan
13th December 2016, 07:14
I am really bad in regular expressions, but with QRegularExpression and with an example in your firs post should work something like this:


QStringList result;

QString str = "(indicators1)somethings&para1=value1&para2=value2 &something";

QRegularExpression exp("((?<=\\()(.*?)(?=\\)))|((?<=\\&)(.*?)(?=\\=))|((?<=\\=)(.*?)(?=\\&))");

QRegularExpressionMatchIterator i = exp.globalMatch(&str);
while (i.hasNext())
{
QRegularExpressionMatch expMatch = i.next();
if (expMatch.hasMatch())
{
result.append(expMatch.captured(0));
}
}
If I understand you correctly.

anda_skoa
13th December 2016, 11:10
If "&" is the separator you could use QStringList::split().

But since you say it is URL encoded, maybe you can parse it with QUrl or QUrlQuery?

Cheers,
_

quantiti
13th December 2016, 15:28
I am really bad in regular expressions, but with QRegularExpression and with an example in your firs post should work something like this:
If I understand you correctly.
Thank you. Let me try...


If "&" is the separator you could use QStringList::split().

But since you say it is URL encoded, maybe you can parse it with QUrl or QUrlQuery?

Cheers,
_
Thank you, that's ideal. But in my case I reading a log file, read line by line and need detect line has data match the regex

Added after 29 minutes:

I need detect string contain "indicator1", "para1=", "para2=" (they have fixed value) and then I capture their values
(eg I need detect website action: http://site.com?INDICATOR.PHP?somethine&ACTION=actiontolog&TARGET=targettolog...)

Added after 40 minutes:


I am really bad in regular expressions, but with QRegularExpression and with an example in your firs post should work something like this:


QStringList result;

QString str = "(indicators1)somethings¶1=value1¶2=value2 &something";

QRegularExpression exp("((?<=\\()(.*?)(?=\\)))|((?<=\\&)(.*?)(?=\\=))|((?<=\\=)(.*?)(?=\\&))");

QRegularExpressionMatchIterator i = exp.globalMatch(&str);
while (i.hasNext())
{
QRegularExpressionMatch expMatch = i.next();
if (expMatch.hasMatch())
{
result.append(expMatch.captured(0));
}
}
If I understand you correctly.

Thank for your suggestion. Now I have code that works as expected


QString str = "haha:site.com:hehe&para1=value1&para2=value2&hihi";
QRegularExpression re("site.com:(.*?)&para1=(?<para1value>.*?)&para2=(?<para2value>.*?)&");
QRegularExpressionMatch match = re.match(&str);
if (match.hasMatch()) {
qDebug()<<match.captured("para1value")<<"|"<<match.captured("para2value");

}

result:


"value1" | "value2"