PDA

View Full Version : QRegExp and quantifiers



paolom
20th December 2011, 12:33
Hi,

I've a QRegExp like this:




QRegExp ex;
ex.setPattern(".{" + QString("%1").arg(mMinLength) + "," + QString("%1").arg(mMaxLength) +"}");



where mMinLength and mMaxLength are the min and max length that I espected to have.

I use this expression to validate both some QString from file and QString from QLineEdit.
If the maxLenght is <= 1024 the QlineEdit works, insteand I can't input.

Is there a limit to max number of quantifiers in QRegExp?

How can avoid this?

Many thanks

fullmetalcoder
20th December 2011, 17:54
I use this expression to validate both some QString from file and QString from QLineEdit.
for the QLineEdit validation, why not use the maxLength property instead?
for the "string from file" validation why use a regexp at all if you only care about the size?

Is there a limit to max number of quantifiers in QRegExp?
I've never bothered to check before but apparently yes, although it is not mentioned in the docs. A quick look at the source (http://qt.gitorious.org/qt/qt/blobs/4.7/src/corelib/tools/qregexp.cpp) tells us that quantifiers are bounded by "InftyRep" which appears to be set to 1025... To be honest I've not dug far enough to check how this constant impact quantifier bounds but its very existence is worrying as it would suggest that even a star quantifier is bounded to as little as 1024 repetition (not that I ever bumped into this but still...).

paolom
20th December 2011, 18:08
Many thanks for your reply.

I need to use this validator ( and not the maxLength ) cause I want to create a unique validator so I can validate a string from the line edit and a string that I read from a file.

So, as your opinion, If I want to control an undefined string length ( > 1024 ) is there a suitable regular expression ??!! ( I think surely yes, but I'm not good with reg exp... :D :D )

fullmetalcoder
20th December 2011, 21:17
I need to use this validator ( and not the maxLength ) cause I want to create a unique validator so I can validate a string from the line edit and a string that I read from a file.

Subclass QValidator to check string length instead of using a QRegExpValidator


So, as your opinion, If I want to control an undefined string length ( > 1024 ) is there a suitable regular expression ??!! ( I think surely yes, but I'm not good with reg exp... :D :D )

QString s(".{%1, 1024}").arg(min);
for (int i = 1; i < max / 1024 - 1; ++i) s += QString(".{0,1024}");
s += QString(".{0,%2}").arg(max % 1024);
But, really, that is fugly...