Solved

How to validate a url

Posted on 2011-09-21
10
234 Views
Last Modified: 2012-05-12
0
Comment
Question by:dvcphp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 36577143
This one:
/^(https?:\/\/)?(\w[\w\-\.]*)+(\/(\w[\w\-\.]*)+)+(\?[\w\=\%\&]*)?(\#[\w]*)?/

Open in new window

will not allow the // after .com as in yout third line example.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36577201
Sorry, ignore the upper.

Check please this:
/^(https?:\/\/)?(\w[\w\-\.]*){3,}(\.(\w[\w\-\.]*){2,}){2,}(\/(\w[\w\-\.]*)+)?(\?[\w\=\%\&]*)?(\#[\w]*)?/

Open in new window

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36577211
Sorry I missed the $ at the end.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

Expert Comment

by:Zvonko
ID: 36577220
If you are extracting URLs the use \b patter instead of string begin and end characters ^and $
0
 

Author Comment

by:dvcphp
ID: 36577249
Hi Zvonko,
I tried the code.It seems to work only with the third condition.I am sorry I mistyped 2 // .It should be one /
Is it possible to have all the 3 conditions working.

Also I need to check the same expression with something below

www.something.org/body.sgs?id=3435123

www.something.org/#!comm.home    and

www.something.org/About+Us/Co+Integrity/Comm+Needs+help
Thanks,
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36577443
Check this please:
/^(https?:\/\/)?(\w[\w\-\.]*){3,}(\.(\w[\w\-\.]*){2,}){2,}(\/(\w+([\w\-\.\+]+\/?)*)*)*(\?[\w\=\%\&\+\.]*)?(\#[\w\!\.\+]*)?$/

Open in new window

0
 

Author Comment

by:dvcphp
ID: 36577473
Thank you so much.That worked like a charm.

Thank you again.You are truely a Genius.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36577488
<|:-)
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 36577518
One additional note: the last expression allows this:
www.something.org#!comm.home 

I wanted to avoid that but my IE8 browser slowed down and after several loops crashed.

This is the expression to force a slash before query or hash url parameters:

/^(https?:\/\/)?(\w[\w\-\.]*){3,}(\.(\w[\w\-\.]*){2,}){2,}(\/(\w+([\w\-\.\+]+\/?)*)*(\?[\w\=\%\&\+\.]*)?(\#[\w\!\.\+]*)?)?$/

Open in new window

0
 

Author Comment

by:dvcphp
ID: 36577543
Thanks for letting me know.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question