Quantcast

Regular Expression To Validate A URL

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Regular Expression To Validate A URL

bdrhoa
I need a regular expression to validate a URL. There are lots of them
out there, but I can't seem to get the escaping right in grails.

For example, I'd like to do:

assert "http://www.grails.org" ==~
/^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$/

I'd be happy with any expression that works.

Thanks!

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regular Expression To Validate A URL

Wolfgang Schell
Hi,

bdrhoa wrote
I need a regular expression to validate a URL. There are lots of them
out there, but I can't seem to get the escaping right in grails.
is this a field in a Domain class? If yes, there is the url() constraint (see http://grails.org/doc/1.1.x/ref/Constraints/url.html). If this is not part of a Domain class, you could create a Command object (see http://grails.org/doc/1.1.x/guide/6.%20The%20Web%20Layer.html#6.1.9%20Command%20Objects) with some constraints. Or you could simply use the class org.apache.commons.validator.UrlValidator directly. It is readily available, as it is used by the url() constraint.

HTH,

Wolfgang
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regular Expression To Validate A URL

tomas lin
In reply to this post by bdrhoa
There is a url constraint you can set on your String properties within
domain objects and command objects - you can look at the source of
that for the right expression.

http://grails.org/doc/latest/ref/Constraints/url.html

T

On Tue, Sep 8, 2009 at 8:22 AM, Brad Rhoads<[hidden email]> wrote:

> I need a regular expression to validate a URL. There are lots of them
> out there, but I can't seem to get the escaping right in grails.
>
> For example, I'd like to do:
>
> assert "http://www.grails.org" ==~
> /^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*$/
>
> I'd be happy with any expression that works.
>
> Thanks!
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regular Expression To Validate A URL

bdrhoa
Thanks for your reply! I can't use that for my situation. My table has
fieldValue and fieldType. fieldValue gets edited against a regular
expression, based on its fieldType.

On Tue, Sep 8, 2009 at 5:34 AM, Tomas Lin<[hidden email]> wrote:

> There is a url constraint you can set on your String properties within
> domain objects and command objects - you can look at the source of
> that for the right expression.
>
> http://grails.org/doc/latest/ref/Constraints/url.html
>
> T
>
> On Tue, Sep 8, 2009 at 8:22 AM, Brad Rhoads<[hidden email]> wrote:
>> I need a regular expression to validate a URL. There are lots of them
>> out there, but I can't seem to get the escaping right in grails.
>>
>> For example, I'd like to do:
>>
>> assert "http://www.grails.org" ==~
>> /^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*$/
>>
>> I'd be happy with any expression that works.
>>
>> Thanks!
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>



--
---------------------------
www.maf.org/rhoads
www.ontherhoads.org

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regular Expression To Validate A URL

Florian Salbrechter
Hi!

I think what Thomas meant is that you should look the validation up in the Grails source.
I just had a look seems pretty straightforward, but I'm not that regex professional.

The Sources important to you are:

org.codehaus.groovy.grails.validation.routines.UrlValidator and
org.codehaus.groovy.grails.validation.UrlConstraint
I think.

You can use this expression:
    /**
     * This expression derived/taken from the BNF for URI (RFC2396).
     */
    private static final String URL_REGEX =
            "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?";
    //                                                                      12            3  4          5       6   7        8 9
    private static final Pattern URL_PATTERN = Pattern.compile(URL_REGEX);
(UrlValidator.java:94)

kind regards,
hth,
flo

bdrhoa wrote
Thanks for your reply! I can't use that for my situation. My table has
fieldValue and fieldType. fieldValue gets edited against a regular
expression, based on its fieldType.

On Tue, Sep 8, 2009 at 5:34 AM, Tomas Lin<tomaslin@gmail.com> wrote:
> There is a url constraint you can set on your String properties within
> domain objects and command objects - you can look at the source of
> that for the right expression.
>
> http://grails.org/doc/latest/ref/Constraints/url.html
>
> T
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Regular Expression To Validate A URL

Florian Salbrechter
BTW: There's a nice program out there called RegexBuddy [1] that will help you build and check your regular expressions and also has some examples and predefined sets!

[1] http://www.regexbuddy.com/
Loading...