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.


  • Moderators

    Have a look to "this thread":http://developer.qt.nokia.com/forums/viewthread/5736

    From this thread the Octet is what you are looking for
    @
    QString Octet = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";
    @



  • Exactly what I needed, thanks!


Log in to reply
 

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