PDA

View Full Version : Can't get the QRegExp I need



roseicollis
30th March 2015, 08:35
Hi! I'm trying to get a RegExp to validate the info of a lineedit which accepts 3 numbers followed by DG and then 4 numbers more and a fifth optional.
For example, valid will be: 123DG1234 or 111DG2222
Invalid ones would be: 111DG or 111DDD or 111 or 111DG222

I have the next but when I try to write, I cant. I doesn't let me write a number or letter or anything. I tried using other RegExp I have for other LineEdits and it works fine so I suppose thta the problem is in the setPattern.


QRegExp rx9;
rx9.setPattern("[1-9]\\d{3}][DG]\\d{4,5}");
QValidator *validator9 = new QRegExpValidator(rx9, this);
myLineEdit->setValidator(validator9);

EDIT: Andif I put

rx9.setPattern("[1-9]{3}[DG][1-9]{4}");
It just allows me to write 123D and that's all

Thank you so much

yeye_olive
30th March 2015, 09:17
There are square brackets (not always balanced) all over the pattern strings you wrote. You should re-read the documentation of QRegExp (especially the paragraph called "Introduction", which describes the syntax and semantics of the square brackets) and you will understand what is going on.

roseicollis
30th March 2015, 10:08
Oh I see, the brackets of DG are the problem. Thank you! I thought that it worked in other way until I re-read it

But I have still another problem, it accepts either 123DG1234 and 123DG12 which is wrong

yeye_olive
30th March 2015, 15:07
But I have still another problem, it accepts either 123DG1234 and 123DG12 which is wrong
Can you post a code sample reproducing the problem? If you get the pattern right, then QRegExpValidator::validate("123DG12", 0) should return QValidator::Intermediate.

roseicollis
30th March 2015, 16:39
Hi yeye thx for your help but Its solved now :) I just had to use exact match in that case only dunno why but... it works so.. Problem solved.

jefftee
30th March 2015, 19:41
I would recommend that you use QRegularExpression instead of QRegExp in any new code you are writing/fixing. QRegularExpression supports Perl Compatible Regular Expression (PCRE) and much better IMHO.