February 26, 2012

Klondike6 Klondike6
Lab Rat
17 posts

QRegExp - Question on defining a regular expression

 

I am using QRegExp to validate user input. I would like the user to input a number from 0 to 255. (Eventually this will be used to validate input of an IP address like 192.168.0.123)

At this point, I have a working expression that allows the user to enter 1-3 digits, each in the range 0-9:
[0-9]{1,1}[0-9]{0,1}[0-9]{0,1}

This works, but allows the user to enter values from 0 to 999. How best to restrict the input to 0 to 255?

If the first digit entered is greater than 2, then we can only allow one more digit.
If the first digit is equal to 2, then if the user enters a total of three digits, the second and third digits cannot exceed 5.

Any guidance is appreciated.

2 replies

February 26, 2012

koahnig koahnig
Mad Scientist
2099 posts

Have a look to this thread [developer.qt.nokia.com]

From this thread the Octet is what you are looking for

  1. QString Octet = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";

February 26, 2012

Klondike6 Klondike6
Lab Rat
17 posts

Exactly what I needed, thanks!

 
  ‹‹ How to setup local proxy server to test Qt QNetworkAccessManager proxy settings?      [Solved]QFile::exists return true when i choose folder ››

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