Cannot find key in INI file with QRegExp



  • Hi friends

    I have the simple INI file. Here some content:
    @
    <ini>
    [ right section too ] // I can found it with section(QString("^\s*[\s*%1\s*]").arg("right section too"));
    data=123 //I try to extract value '123' with keyLine(QString("^\s*%1\s*=\s*(.)\s((//)|#.*)?").arg("data"))
    word = word # bla bla bla
    </ini>
    @
    Now code's pieces:

    @
    QRegExp section(QString("^\s*[\s*%1\s*]").arg("right section too"));
    QREgexp keyLine(QString("^\s*%1\s*=\s*(.)\s((//)|#.*)?").arg("data"));

    QStringList fileContent = getFileContent(iniFileName);
    QStringList::iterator item = fileContent.begin();
    QStringList::iterator atEnd = fileContent.end();

    while(item != atEnd)
    {
    if(section.indexIn(*item) //here we found the section
    {
    ++item; //pass section goto next line
    while(item != atEnd) //I know you'll lose next sections
    {
    if(keyLine.indexIn(*item)
    {
    printf("%s\n", item->toAscii().data());
    for(int i = 1; i < keyLine.captureCount(); ++i)
    printf("%d: '%s'\n", i, keyLine.cap(i));
    }
    ++item;
    }
    }
    ++item;
    }
    @

    I was wander when I seen

    @
    word = word # bla bla bla
    1: ''
    2: ''
    @

    When I change keyLine to
    @
    keyLine(QString("^\s*%1\s*=\s*(.)\s((//)|#.*)?\$").arg("data"));
    @
    I seen

    @data=123 //I try to extract value '123' with keyLine(QString("^\s*%1\s*=\s*(.)\s((//)|#.)?").arg("data"))
    1: ''
    2: ''
    @
    When I feed egrep with
    @
    "^\s
    data\s*=\s*(.)\s((//)|#.*)?"
    @
    it found all line entries

    So Q: where I wrong?

    Regards

    PS
    My real code is differ I show but the idea is same

    Edit: Please use @ tags around code sections; Andre



  • In preview the above post looks better



  • When I change keyLine from
    @
    (QString("^\s*%1\s*=\s*(.)\s((//)|#.)?").arg("data"))
    @
    to
    @
    (QString("^\s
    %1\s*=\s*(.\)\s((//)|#.\*)?").arg("data"))
    @
    I seen

    data=123 //I try to extract value...

    but capturedTexts().at(1).toAscii().data() == ''

    So how to get found value??



  • Just wondering: why are you using RegExps to try to parse .ini files in the first place? Would [[doc:QSettings]] not work much better for such a task?



  • Yes, you're righ. But what is with my question?
    I look for example on regexp. It works fine except for finish '?'.
    So question for
    @
    pattern("^\sdata\s=\s*(.)\s((//)|#.*)?");
    @
    takes the place.



  • Quantifiers in [[Doc:QRegExp]] are greedy by default. I did not check it, but "(.*)" matches everything until the end of the line. You might want to change this to "([^#]*)" which excludes the hash sign from the match.



  • Yehh
    I divide query onto 2 parts
    @
    QString CInifile::_getValue(const QString & section, const QString &key)
    {
    QStringList::const_iterator item = _getSectionItem(section);
    QStringList::const_iterator atEnd = _inifileContent.constEnd();
    if(item == atEnd)
    return QString::null;

    QRegExp rxWithComment(QString("^\s*%1\s*=(\s*)(.+)(?=\s)(\s+)(//)|#.").arg(key));
    QRegExp rxWithoutComment(QString("^\s
    %1\s*=(\s*)(.+)(?=\s)(\s+)").arg(key));
    QRegExp *rx;

    while(++item != atEnd)
    {
    rx = &rxWithoutComment;
    if((item->indexOf("//") >= 0) || (item->indexOf("//") >= 0))
    rx = &rxWithComment;

    if(rx->indexIn(*item) > 0)
    return rx->cap(2);
    }
    return QString::null;
    }
    @

    Question to Andre
    Some time ago I worked with the MySql NDB cluster. In my.ini there were N [mysqld] sections where N equals number of MySql servers in the cluster. So in which section of N QSettings will store a value, 4Ex,
    @
    QSettings::setValue("mysqld/port", 1234);
    @
    ?


Log in to reply
 

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