Results 1 to 7 of 7

Thread: QRegExp for extracting the string from log

  1. #1
    Join Date
    Dec 2016
    Posts
    4
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default QRegExp for extracting the string from log

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QRegExp for extracting the string from log

    Quote Originally Posted by quantiti View Post
    value1, value2 can contain any charactor
    Any character? Even "&"?

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    quantiti (12th December 2016)

  4. #3
    Join Date
    Dec 2016
    Posts
    4
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QRegExp for extracting the string from log

    Thank you admin.
    Value has been encoded by url encode. such as @ -> %40, & ->%25
    So value contains a-zA-Z.%

  5. #4
    Join Date
    Dec 2016
    Posts
    4
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QRegExp for extracting the string from log

    could anyone help, please

  6. #5
    Join Date
    Dec 2016
    Location
    Russia
    Posts
    4
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Re: QRegExp for extracting the string from log

    I am really bad in regular expressions, but with QRegularExpression and with an example in your firs post should work something like this:
    Qt Code:
    1. QStringList result;
    2.  
    3. QString str = "(indicators1)somethings&para1=value1&para2=value2 &something";
    4.  
    5. QRegularExpression exp("((?<=\\()(.*?)(?=\\)))|((?<=\\&)(.*?)(?=\\=))|((?<=\\=)(.*?)(?=\\&))");
    6.  
    7. QRegularExpressionMatchIterator i = exp.globalMatch(&str);
    8. while (i.hasNext())
    9. {
    10. QRegularExpressionMatch expMatch = i.next();
    11. if (expMatch.hasMatch())
    12. {
    13. result.append(expMatch.captured(0));
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    If I understand you correctly.

  7. The following user says thank you to MatrixSan for this useful post:

    quantiti (13th December 2016)

  8. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QRegExp for extracting the string from log

    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,
    _

  9. The following user says thank you to anda_skoa for this useful post:

    quantiti (13th December 2016)

  10. #7
    Join Date
    Dec 2016
    Posts
    4
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QRegExp for extracting the string from log

    Quote Originally Posted by MatrixSan View Post
    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...

    Quote Originally Posted by anda_skoa View Post
    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?someth...ET=targettolog...)


    Added after 40 minutes:


    Quote Originally Posted by MatrixSan View Post
    I am really bad in regular expressions, but with QRegularExpression and with an example in your firs post should work something like this:
    Qt Code:
    1. QStringList result;
    2.  
    3. QString str = "(indicators1)somethings¶1=value1¶2=value2 &something";
    4.  
    5. QRegularExpression exp("((?<=\\()(.*?)(?=\\)))|((?<=\\&)(.*?)(?=\\=))|((?<=\\=)(.*?)(?=\\&))");
    6.  
    7. QRegularExpressionMatchIterator i = exp.globalMatch(&str);
    8. while (i.hasNext())
    9. {
    10. QRegularExpressionMatch expMatch = i.next();
    11. if (expMatch.hasMatch())
    12. {
    13. result.append(expMatch.captured(0));
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    If I understand you correctly.
    Thank for your suggestion. Now I have code that works as expected
    Qt Code:
    1. QString str = "haha:site.com:hehe&para1=value1&para2=value2&hihi";
    2. QRegularExpression re("site.com:(.*?)&para1=(?<para1value>.*?)&para2=(?<para2value>.*?)&");
    3. QRegularExpressionMatch match = re.match(&str);
    4. if (match.hasMatch()) {
    5. qDebug()<<match.captured("para1value")<<"|"<<match.captured("para2value");
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 
    result:
    Qt Code:
    1. "value1" | "value2"
    To copy to clipboard, switch view to plain text mode 
    Last edited by quantiti; 13th December 2016 at 16:28.

Similar Threads

  1. Self-extracting archive
    By atomic in forum Qt Programming
    Replies: 7
    Last Post: 8th April 2015, 22:55
  2. Replies: 3
    Last Post: 17th April 2012, 17:05
  3. QRegExp: get the parsed string
    By trallallero in forum Qt Programming
    Replies: 6
    Last Post: 5th April 2012, 07:18
  4. QRegExp for extracting the string between two HTML tags...
    By tuthmosis in forum Qt Programming
    Replies: 3
    Last Post: 27th May 2010, 07:55
  5. qregexp to check string
    By mattia in forum Newbie
    Replies: 3
    Last Post: 19th February 2008, 15:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.