Important: Please read the Qt Code of Conduct -

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

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

    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.

  • Have a look to "this thread":

    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