December 14, 2011

Flurite Flurite
Ant Farmer
92 posts

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?

7 replies

December 14, 2011

p-himik p-himik
Lab Rat
255 posts

Did you read QRegExp class reference [developer.qt.nokia.com] ?

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

December 14, 2011

Flurite Flurite
Ant Farmer
92 posts

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?

December 14, 2011

peppe peppe
Ant Farmer
1025 posts

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

 Signature 

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

December 15, 2011

Flurite Flurite
Ant Farmer
92 posts

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

What is setPattern and how do I use it?

December 15, 2011

p-himik p-himik
Lab Rat
255 posts

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

December 15, 2011

Chris H Chris H
Lab Rat
139 posts

In particular, have a look at the Code Examples [developer.qt.nokia.com] 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).

December 24, 2011

010110102 010110102
Lab Rat
18 posts

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

  1. proxy->setSourceModel(sourcemodel);
  2.  
  3. //The following regexpression filters the column after "NuMbeR1 OR Test3122 OR Dethklok".
  4. //I hope the confusion is solved now.
  5. QRegExp regExp("NuMbeR1|Test3122|Dethklok");
  6. proxy->setFilterRegExp(regExp);
  7.  
  8. //e.g...
  9. ui->view->setModel(proxy);

 
  ‹‹ QGraphicsScene itemAt - allow radius around point?      How to make win32 window as a parent of Qwidget window. ››

You must log in to post a reply. Not a member yet? Register here!