Link to home
Start Free TrialLog in
Avatar of mauricerichard
mauricerichard

asked on

Regular Expressions - web address

Hello Experts,

I need a regular expression that'll check for a valid web address, but it needs to be very ... accepting.  Essentially, anything should match as follows:

1) Must begin with http://
2) Must alllow for any amount of these characters:   / - _?=&
3) Must contain letters
4) 1 to x periods are required

That's it, if anyone can whip me up an expression I've be grateful ... sadly I'm Reg Ex challenged.

Thanks,
Moe
Avatar of petr_hlucin
petr_hlucin

a) Why don't use use one of many RegExes for URL validating on the Internet (e.g. http://snippets.dzone.com/posts/show/452)?

b) The RegEx you describe should look like the following one (except for 4) which I don't understand). Maybe you should also add numeric values.
http://[a-z.]+[a-z/-?=&]*

Open in new window

Avatar of mauricerichard

ASKER

Hi Petr,

Thanks for the RegEx.  I tried many, many RegEx snippets online but they werern't validating correctly for me.  We have some oddball URLs, and I've been asked to make validation very forgiving.

#4 meant that one period was required, but it wasn't limited to one period.

Thanks,
Moe
Hi Petr,

It would seem that the hyphen character is causing this validation to fail.
Not sure why, as I see it in the Reg Ex.  Any thoughts on how to fix this?

Thanks,
Moe
Sorry, I forgot that hyphen is a special char in RegEx - use escape sequence as shown in the attached code snippet.
http://[a-z.]+[a-z/\-?=&]*

Open in new window

Hi Petr,

No worries, I'm just glad to be getting some help on this.  The above RegEx seems to be failing altogether.  Let me give you two examples of URLs I'm using for testing.

ex1: http://website.subsite.k-sites.com/
ex2: Http://server/folder/webpage.aspx?ID=716&TEL=555-666-7777

I appreciate the help, I've been at this for hours and I'm entirely frustrated.
ASKER CERTIFIED SOLUTION
Avatar of petr_hlucin
petr_hlucin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You sir, are brilliant.  This mostly works now.

All that is left is to ensure that a minimum of one period is used, and that some letters are typed as well.  Is that difficult?
What do you mean by period? If you mean part of the string in front of the first "/" (disregarding "http://") then use this one. It solves the miminum of some letter behind "http://".
[hH][tT][tT][pP]://[a-zA-Z0-9.\-]+/[a-zA-Z0-9./\-?=&]*

Open in new window

By period I mean dot ... as in: .com

But you've given me more than enough to work with here, I should be able to muddle through the rest.  Thanks a million for the help!