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. Can I see some simple examples of regular expressions in C++?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 9.5k Views 1 Watching
  • 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.
  • F Offline
    F Offline
    Flurite
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p-himik
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Flurite
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on last edited by
          #4

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

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Flurite
            wrote on last edited by
            #5

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

            What is setPattern and how do I use it?

            1 Reply Last reply
            0
            • P Offline
              P Offline
              p-himik
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Chris H
                wrote on last edited by
                #7

                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).

                1 Reply Last reply
                0
                • 0 Offline
                  0 Offline
                  010110102
                  wrote on last edited by
                  #8

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

                  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