PDA

View Full Version : Expression in QRegExp



lyucs
26th May 2009, 15:11
hello,
I've been trying to understand the meaning of the following expression, but even with QtAssistant and QRegExp documentation, I could'nt get it.

QRegExp test(QLatin1String("^[a-zA-Z]+\\:.*"));

What I know:
the word matched has to start with a sequence of caracters from a to z or A to Z (can I change [a-zA-Z] to \\D instead?)

from \\: and on, I don't understand.
this String has to be an Url, I think.

thank you.

fullmetalcoder
26th May 2009, 16:25
QRegExp docs are quite clear IMO. This regexp will match any string composed of :


a sequence of one or more letters (basic latin alphabet)
a colon ( ':' ) the (double due to C string escaping) backslash prevents this character from having any special meaning (though I don't remember it having one, well it will work anyway...)
a sequence of ANY characters of ANY length (zero or more)

You could of course replace the set by \\D (non-digit class) but it might make more sense to use \\w (word class).

In a nutshell, this regexp certainly does NOT match URLs. The simplest description is that it matches key:value pairs key being made of latin letters and value of any characters.

lyucs
28th May 2009, 12:37
hum. its weird this doesnt match Urls... anyways, thanks!

wysota
28th May 2009, 13:39
It will match URLs (ones with schemas) but only by accident (it will match u bunch of other things as well). If you want an expression for matching URLs then something like this is better:


QRegExp("[A-Za-z][A-Za-z0-9]*:\\/\\/.*")

It will at least make sure there are two slashes behind the colon. And it will allow schemas containing digits. I escaped the slashes themselves "just in case".

lyucs
28th May 2009, 13:53
Oh, that will help
thank you.