Results 1 to 4 of 4

Thread: Creating QRegExp

  1. #1
    Join Date
    Apr 2016
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Creating QRegExp

    Hi,
    Please help me on doing this.
    I have a lineedit in which user can enter something like 12-24,67-90.
    Actually user is entering range of values. If user is entering anything other than numbers,hyphen and comma it should pop up data is not valid.
    How to form a QRegExp for this and is it a right way to check it using ui->lineEdit->hasAcceptableInput function?

    Thanks in advance,
    Namitha

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Creating QRegExp

    Look at QRegExpValidator or QRegularExpressionValidator. You can create an instance of that class and install it on your line edit. The regular expression you use will depend on the requirements on the user input. For the string you have indicated above, a regular expression that will accept it is:

    Qt Code:
    1. ^[1-9][0-9]*-[1-9][0-9]*\,[1-9][0-9]*-[1-9][0-9]*
    To copy to clipboard, switch view to plain text mode 

    which assumes that there can be one- or two-digit numbers and that a leading zero is not required. It also assumes that a range is required. A string such as "9-20,22" will not pass, for example. On the other hand, it will accept "20-9,8-1", which might not be what you want. You can also use regular expressions with QString::split() to extract the pieces of the line input text once it has passed validation.

    Qt's regular expression syntax is based on Perl. You can read about Perl's RE syntax here.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2016
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Creating QRegExp

    Hi,
    Thank you for the reply. I am going to look on Qt RegExp syntax.
    Namitha

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Creating QRegExp

    I would recommend QRegularExpression over QRegExp for new development. The regex below fits your requirement I believe:
    Qt Code:
    1. ^(?:\d+(?:-\d+)*)(?:,\d+(?:-\d+)*)*$
    To copy to clipboard, switch view to plain text mode 
    It will match on any of the following:
    Qt Code:
    1. 1,2,3
    2. 11,22,33
    3. 1-2
    4. 1-2,3
    5. 1,2-3,4
    6. 1-2,3-4
    7. 1,2,3-4
    To copy to clipboard, switch view to plain text mode 
    The regex shown may also work with QRegExp, but I didn't try it.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  5. The following user says thank you to jefftee for this useful post:

    d_stranz (17th November 2016)

Similar Threads

  1. QRegExp Help
    By ToddAtWSU in forum Qt Programming
    Replies: 6
    Last Post: 16th November 2010, 15:35
  2. QRegExp Help
    By Ahmad in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2007, 00:13
  3. need help for my QRegExp
    By patcito in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2006, 16:29
  4. QRegExp
    By evgenM in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2006, 14:22
  5. QRegExp
    By evgenM in forum Newbie
    Replies: 2
    Last Post: 24th March 2006, 12:52

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.