2

I am writing a regular expression checker for QLineEdit and I am using QRegExp.

I have already wrote a int value checker:

QRegExp *expression_ = new QRegExp("^(0|[1-9]{1,1}[0-9]{0,9});

But I have complications with double values, to be more specific, I cannot separate the case of this numbers. F.E 0.210 or 0.001.

Please help me to implement this feature for double values. Thanks in advance.

Eduard Rostomyan
  • 7,050
  • 2
  • 37
  • 76
  • what's your input, your current output and your desired output? Also, what's `[1-9]{1,1}` supposed to mean? :) – Robin Mar 31 '14 at 13:02
  • The input is a string. QRegExp checks whether the string can be represented as int value. The 0|[1-9]{1,1} means that if the numbers can either start with 0 and no extra digits, or can start with one of the digits 1-9. – Eduard Rostomyan Mar 31 '14 at 13:05
  • I'm glad to know you're using regex on strings, but what is the pattern you want to match? Please provide a few example of these strings along with what you want to match in them. What does it mean, "I cannot separate the case of this numbers"? What are you trying to do? Please edit your question to add more info on these kind of questions. – Robin Mar 31 '14 at 13:20
  • You just want to check if your string is an integer? – Robin Mar 31 '14 at 13:25
  • not integer, a double – Eduard Rostomyan Mar 31 '14 at 13:37
  • What pattern do you want to match? What rules for your regex? Please give examples by editing your question. FYI `[1-9]{1,1}` means `[1-9]` between one and one time. so exactly the same as just `[1-9]`. – Robin Mar 31 '14 at 13:52

5 Answers5

9

This is what I'm using as a RegEx String for positive/negative float values

[+-]?\\d*\\.?\\d+
Bowdzone
  • 3,827
  • 11
  • 39
  • 52
6

Looks like you are trying to reinvent the wheel. See: QDoubleValidator, QIntValidator.

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • I have had many issues with the Qt Validators, for example the double validator gets confused with the comma and the dot. I found QIntValidator to also work peculiar, it accepts the comma in the field, but then removes it so 23,2 becomes 232. also if you type 23,2 and are out of bounds it will move the comma to 2,32 ... just not what you want to use. I found writing your own validator class and overwriting the validate function (that's where that regex goes) the way to go to ensure correctness. – Alexandra Anghelescu Jan 22 '16 at 15:29
  • 1
    If you are not aware about locale setting than you can become confused. I've seen many developers hard-coding decimal separator and the fighting with strange bugs since they do not understand that good application should support other languages too and those other languages have different roles for numbers. – Marek R Jan 24 '16 at 21:48
0

Try this regex for decimal numbers:

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

Using ?: the group capture is ignored here.

Try the online demo

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • I find this much more accurate, since leading zeros are not allowed. Anyway, i would expand it to `^-?(?:0|[1-9][0-9]*)\.?[0-9]+$`, so that negative values and values without a point are catched too. Thanks for the link - very helpful! – AquilaRapax Jan 27 '17 at 09:03
0

I had same issue, following expression helped to solve my task, try it: \\d*\.\\d*

It helps to get numbers like "0." too (without fractional part).

-2

In order to manage exponential data:

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