PDA

View Full Version : Implement a regex in QRegExp



Alir3z4
27th April 2012, 01:59
Here is the regex and how i use it.

QString regexPattern =
"^(?:http|ftp)s?://" // http:// or https://
"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\\.)+(?:[A-Z]{2,6}\\.?|[A-Z0-9-]{2,}\\.?)|" // #domain...
"localhost|" // localhost...
"\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})" // ...or ip
"(?::\\d+)?" // optional port
"(?:/?|[/?]\\S+)$";
_regexValidator = new QRegExpValidator(QRegExp(regexPattern), this->parent());


Problem is that the QRegExpValidator::validate() never returns Acceptable.
I don't know what part i missing in it.
Also the regex is for URL validation ;)

For the record, the regular experssion itself is stolen from django.core.validators.URLValidator (https://code.djangoproject.com/browser/django/trunk/django/core/validators.py#L36)

ChrisW67
27th April 2012, 02:49
For a start your regular expression is broken and your compiler is probably warning you about an unknown escape "\.". You need to double-up the \\ before the periods in the domain name and ip address.

Once you have that fixed, your domain name would have to be all upper-case.

Alir3z4
27th April 2012, 02:57
For a start your regular expression is broken and your compiler is probably warning you about an unknown escape "\.". You need to double-up the \\ before the periods in the domain name and ip address.

Once you have that fixed, your domain name would have to be all upper-case.

Actually no, the compiler didn't warn me about unknown escape, beside that i did try also double slash \\. but no didn't work.
As you said i tried to make domain In upper-case, but the results was same !
And something else can somehow make that regex case-insensitive by the QRexExp itself?

Update:
Yes you were right, i just:

_regexValidator = new QRegExpValidator(QRegExp(regexPattern, Qt::CaseInsensitive), this->parent());
and make it CaseInsensitive :D, and now it works. i should test it more.
Thanks ChrisW67