1

I need to validate given string from QLineEdit. True Input: 355.12° L

The double number must be between 0-360, and the last character must be L or R. I have used QString mask for degree(°) and this example for 360 but i can't use mask and QDoubleValidator together. Looks like only the way is QRegExp to solve this problem.

My mask string:

">999.99°A"
Community
  • 1
  • 1
Ulfsark
  • 902
  • 1
  • 11
  • 26

2 Answers2

1
((?:[012]?[0-9]{1,2}|3(?:[0-5][0-9]|60))(?:\.[0-9]{0,2})?)°[LR]

Try the above pattern. I'm not sure if qregexp supports (?:) patterns or not though.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
1

The pattern I suggest is deliberately long and redundant to be the most efficient as possible (the goal is to reduce the most possible the regex engine work):

QRegExp exp("^(?:[1-2][0-9]{0,2}(?:\\.[0-9]{1,2})?|3(?:[0-5]?[0-9]?(?:\\.[0-9]{1,2})?|60(?:\\.00?)?)|[4-9][0-9]?(?:\\.[0-9]{1,2})?|0(?:\\.[0-9]{1,2})?)° ?[LR]$");
lineEdit_->setValidator(new QRegExpValidator(exp, this));

This pattern forbids leading zeros for tens and hundreds and makes optional the decimals that limited to two digits (thus, a trailing or a leading dot is not allowed) An optional space is allowed between ° and L (or R)

Now, if you need a pattern that fits exactly the mask 999.99°A (i.e. leading zeros are correct, there are no optional spaces anywhere, there is always three digits and two decimal digits), you can use this pattern instead of the previous:

^(?:[0-2][0-9]{2}\\.[0-9]{2}|3(?:[0-5][0-9]\\.[0-9]{2}|60\\.00))°[LR]$
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
  • You've probably found this too: there seems to me a bias toward short regexes, with the idea that "shorter is better". And as you know that can be true but also very false... And it can be discouraging as you know that if you use atomics etc, the shorter slower version will be picked. :) So, +1 for this unrewarded work. – zx81 Jul 11 '14 at 00:04
  • @zx81: Thank you for your encouragement. The main thing is that this pattern resonates somewhere. – Casimir et Hippolyte Jul 11 '14 at 20:33