Solved Getting Litterals ., + and - in a QRegExp
-
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...
-
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]*)?))$"
-
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?
-
Do you mean the QRegularExpressionValidator ?
-
Yes, that's what I missed. Thanks! I will use QRegularExpression instead of QRegExp :)