Can I see some simple examples of regular expressions in C++?



  • I'm trying to search for some good examples of regular expressions in C++, so I came here. Nothing really complex, just simple, because I'm learning. Maybe some example code for checking whether the first two characters of a string are digits, and whether the rest are lower-case letters? Really, anything would be fine.

    I also have another question about QRegExp. Do I have to use it with Qt Creator, or can I just do it the classic way?



  • Did you read "QRegExp class reference":http://developer.qt.nokia.com/doc/qt-4.7/qregexp.html ?

    Qt libraries are not bound with Qt Creator so you can use QRegExp with any IDE.



  • Okay, thanks for linking me there. I have a question about setting QRegExp values though, because it seems that you can't set them using the '=' sign. For example, this works:

    QRegExp regexp1("^[0-9]*$");

    ..but this doesn't..

    QRegExp regexp1 = "^[0-9]*$";

    Are there any reasons why they don't work? And since I'm used to using an '=' sign to set my values, is there any way I can still set them using the '=' operator?



  • What's wrong with using setPattern? A regular expression is not a string.



  • bq. What’s wrong with using setPattern? A regular expression is not a string. bq.

    What is setPattern and how do I use it?



  • I repeat myself - did you read the documentation? setPattern is member function of QRegExp class which allows you to, obviously, set pattern.



  • In particular, have a look at the "Code Examples":http://developer.qt.nokia.com/doc/qt-4.7/qregexp.html#id-a175aa6e-68e1-4532-b4fe-f9538e531bbc section, which I think probably has the simple sort of regex examples you are looking for (assuming you already know a bit about regular expressions, and are just looking for info on implementing them using QRegExp).



  • Just to be absolutely clear here... have a look at this:

    @
    QSortFilterProxyModel *proxy=new QSortFilterProxyModel();
    proxy->setSourceModel(sourcemodel);

    //The following regexpression filters the column after "NuMbeR1 OR Test3122 OR Dethklok".
    //I hope the confusion is solved now.
    QRegExp regExp("NuMbeR1|Test3122|Dethklok");
    proxy->setFilterRegExp(regExp);

    //e.g...
    ui->view->setModel(proxy);
    @


Log in to reply
 

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