Results 1 to 7 of 7

Thread: QRegExp: Last sign has to be a number

  1. #1
    Join Date
    Jan 2016
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default QRegExp: Last sign has to be a number

    Hello,

    i need a regular expression that ensures that the last sign in a QString is a digit and i tried the following code:


    Qt Code:
    1. QRegExp rx ("[0-9]$");
    2.  
    3. cout << rx.exactMatch("123") << endl;
    To copy to clipboard, switch view to plain text mode 

    i always get false as result, but i don't understand why?

    Can anybody tell me what i'm doing wrong?

    Thank you very much!

  2. #2
    Join Date
    Jan 2006
    Posts
    33
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: QRegExp: Last sign has to be a number

    Can't test but I think this should work:

    Qt Code:
    1. QRegExp rx (".*[0-9]$");
    To copy to clipboard, switch view to plain text mode 


    Your regexp should match this:
    Qt Code:
    1. cout << rx.exactMatch("3") << endl;
    To copy to clipboard, switch view to plain text mode 
    Does this match?
    If so, [0-9]$ machtes one and only one digit (exactMatch)
    [0-9] can be written as \d as well, just follow the link to QRegExp
    Last edited by Moppel; 19th January 2016 at 14:48.

  3. #3
    Join Date
    Jan 2016
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QRegExp: Last sign has to be a number

    Thank you for your answer! But with this i get an invalid expression. The error string is:

    not valid!: bad repetition syntax

    Added after 47 minutes:


    Ah, it works with

    Qt Code:
    1. QRegExp rx (".*[0-9]$");
    To copy to clipboard, switch view to plain text mode 

    Thank you very much, Moppel and have a nice day!!
    Last edited by forenued; 19th January 2016 at 14:44.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QRegExp: Last sign has to be a number

    Alternatively you could check if the last character is a digit.

    Cheers,
    _

  5. #5
    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: QRegExp: Last sign has to be a number

    With any new code you should look at and use (IMHO) QRegularExpression over QRegExp.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QRegExp: Last sign has to be a number

    I agree but using any kind of regular expression looks wasteful if the requirement is to just check the last character, which is basically just an "isDigit" check on the last QChar in the string.

    Cheers,
    _

  7. #7
    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: QRegExp: Last sign has to be a number

    Quote Originally Posted by anda_skoa View Post
    I agree but using any kind of regular expression looks wasteful if the requirement is to just check the last character, which is basically just an "isDigit" check on the last QChar in the string.
    I agree, just couldn't tell if it was a homework assignment for regular expressions, etc!
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. QTableView replace row number with sign
    By Hostel in forum Qt Programming
    Replies: 6
    Last Post: 22nd May 2013, 21:39
  2. Replies: 1
    Last Post: 5th March 2012, 06:34
  3. how to sign my .msi file
    By athulms in forum Newbie
    Replies: 4
    Last Post: 28th September 2011, 07:10
  4. How to free sign my app ?
    By Sparkle24 in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 21st August 2011, 23:25

Tags for this Thread

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.