Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting Litterals ., + and - in a QRegExp
Forum Updated to NodeBB v4.3 + New Features

Getting Litterals ., + and - in a QRegExp

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ModelTechM Offline
    ModelTechM Offline
    ModelTech
    wrote on last edited by
    #1

    I have this humongous regular expression to express any positive integer or real number (I tested it on http://regexr.com/):

    ^\+?((0*[1-9][0-9]*(\.[0-9]+)?((e|E)?[\+|\-]?[1-9][0-9]*)?)|([0-9]+\.[0-9]*[1-9][0-9]*((e|E)?(\+|\-)?[0-9]*[1-9][0-9]*)?))$
    

    My question is: how do I make this into a valid QRegExp? Using it as above is not accepted as Qt complains that ., + and - are not recognized as valid escape sequences...

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Are you locked to QRegExp ? It has been deprecated. QRegularExpression would be more suited.

      As for your question, you need to escape them using two backslashes.

      "^\\+?((0*[1-9][0-9]*(\\.[0-9]+)?((e|E)?[\\+|\\-]?[1-9][0-9]*)?)|([0-9]+\\.[0-9]*[1-9][0-9]*((e|E)?(\\+|\\-)?[0-9]*[1-9][0-9]*)?))$"

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • ModelTechM Offline
        ModelTechM Offline
        ModelTech
        wrote on last edited by
        #3

        Thanks! The extra backslashes work :)

        I was not aware of QRegularExpression. I just tried to use it but there seems to be no replacement for QRegExpValidator, which I need as argument for QLineEdit::setValidator. Or am I missing something?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Do you mean the QRegularExpressionValidator ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • ModelTechM Offline
            ModelTechM Offline
            ModelTech
            wrote on last edited by
            #5

            Yes, that's what I missed. Thanks! I will use QRegularExpression instead of QRegExp :)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved