Results 1 to 4 of 4

Thread: IP adresses with QRegExp (Qt3)

  1. #1
    Join Date
    Jan 2006
    Posts
    156
    Thanked 12 Times in 12 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default IP adresses with QRegExp (Qt3)

    Hello,

    I want to define a mask for QLineEdit to match IP addresses. I want to prohibit the 0 front the number entered. With QRegExp how to allow 1 but not 01 nor 001, 21 but not 021 etc.

    Thanks,

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: IP adresses with QRegExp (Qt3)

    set input mask to "D00.D00.D00.D00"

    Of course this doesn't protect from entering an invalid IP, like 999.999.999.999 and also forbids from entering 0.0.0.0 or 192.168.0.1

    So maybe it would be better to use a regexp validator instead of input mask and/or substitute those 'D's with '0's.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: IP adresses with QRegExp (Qt3)

    Quote Originally Posted by jlbrd
    With QRegExp how to allow 1 but not 01 nor 001, 21 but not 021 etc
    It should be something like this:
    Qt Code:
    1. (?:(?:[01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){3}(?:[01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Posts
    156
    Thanked 12 Times in 12 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: IP adresses with QRegExp (Qt3)

    Here a solution:

    Qt Code:
    1. QRegExpValidator* ValIPAddr;
    2. QRegExpValidator* ValNetMask;
    3. // match the Bytes can be from 0-199 or 200-249 or 250-255 but not a number with one 0 at the beginning like 001 or 020
    4. QString Byte = "(?!0[0-9])(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";
    5. QRegExp rxip;
    6. rxip.setPattern("^" + Byte + "\\." + Byte + "\\." + Byte + "\\." +
    7. Byte + "$");
    8. ValIPAddr = new QRegExpValidator(rxip, 0);
    9. QRegExp rxnetmask;
    10. rxnetmask.setPattern("^255\\.255\\.255\\.(?:255|254|252|248|240|224|192|128|0)$|^255\\.255\\.(?:254|252|248|240|224|192|128|0)\\.0$|^255\\.(?:254|252|248|240|224|192|128|0)\\.0\\.0$|^(?:254|252|248|240|224|192|128|0)\\.0\\.0\\.0$");
    11. ValNetMask = new QRegExpValidator(rxnetmask, 0);
    12. ip->setValidator(ValIPAddr);
    13. mask->setValidator(ValNetMask);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Two Word QRegExp Help
    By mabeeh in forum Qt Programming
    Replies: 3
    Last Post: 13th May 2008, 14:29
  2. QRegExp Help
    By Ahmad in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2007, 00:13
  3. QRegExp progblem
    By high_flyer in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2006, 12:12
  4. need help for my QRegExp
    By patcito in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2006, 16:29
  5. Trouble parsing using simple QRegExp
    By johnny_sparx in forum Qt Programming
    Replies: 4
    Last Post: 24th February 2006, 00:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.