Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot find key in INI file with QRegExp
QtWS25 Last Chance

Cannot find key in INI file with QRegExp

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    js_cpp
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • J Offline
      J Offline
      js_cpp
      wrote on last edited by
      #2

      In preview the above post looks better

      1 Reply Last reply
      0
      • J Offline
        J Offline
        js_cpp
        wrote on last edited by
        #3

        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??

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • J Offline
            J Offline
            js_cpp
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              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.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • J Offline
                J Offline
                js_cpp
                wrote on last edited by
                #7

                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);
                @
                ?

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved