Regular Expression



  • @#include <QCoreApplication>
    #include<QRegExp>
    #include<QString>
    #include<QDebug>
    #include<QRegularExpression>
    #include<QRegularExpressionMatch>
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    QString str="46.33.247.139 - - [05/Jan/2014:16:03:32 -0800] \\"GET /logs/access_130930.log HTTP/1.0" 200 1251943 "http://cytoteconline.jw.lt/200mcg" "Opera/9.80 (Windows NT 6.2; WOW64) Presto/2.12.388 Version/12.11" "redlug.com"";
    RegExp rx ("/([\d.]+)|[([^[]+)]|"([^"]+)"/");
    rx.indexIn(str);
    qDebug() <<"String:"<< rx.cap(0);
    qDebug() <<"IP Adrs:"<< rx.cap(1);
    qDebug() << "Date & Time:"<<rx.cap(2);
    qDebug()<<"Aceess:"<<rx.cap(3);
    qDebug()<<"Size:"<<rx.cap(4);
    qDebug()<<rx.cap(5);
    qDebug()<<rx.cap(6);
    qDebug()<<rx.cap(7);
    return a.exec();
    }@
    Hi, i have to extract the fields from the string.Am using the regular expression for extraction.But it is not working correctly.Already test my regular expression in online regexp tester tool,at that time it match the string.My problem is that extraction is not working correctly.How it is possible? Any idea?



  • Check your regular expression
    from Qt Assistant:
    Note: The C++ compiler transforms backslashes in strings. To include a \ in a regexp, enter it twice, i.e. \. To match the backslash character itself, enter it four times, i.e. \\.
    not [\d] but [\d]


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Please enclose your code in coding tags (one @ at the beginning and one at the end) to make it readable.

    As AntonEml said, your reg exp is not valid. Check Qt's documentation for the subtleties. Also you can build the examples/tools/regexp example program to help you write the correct regexp



  • ok.. Thanks


  • Moderators

    Also, use QRegularExpression instead of QRegExp. The latter is using a custom engine and is not fully compatible with Perl regexp syntax (so it may work different from your online checking tool). QRegularExpression, on the other hand, is based on PCRE and is basically a "has-it-all" package :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.