0

I am using a regular expression for validation web url. that is not validating more than one dots(.) like www.gmail.....com, other than this it is working fine.can any body update my regular expression-

^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$
7
  • 1
    Which language/regex facility? Commented Dec 13, 2010 at 11:50
  • 1
    You should change web url to domain name Commented Dec 13, 2010 at 11:51
  • URL's can contain a lot of characters. Not only a-z. Characters like ü could appear, too. And you should match case-insensitive instead of using com|COM (think about cOM or Com, which are valid, too). Commented Dec 13, 2010 at 11:52
  • Your top-level domain part needs to account for the many country codes (.uk, .es, .us, .ca, .au and the 200-odd others) as well as other TLDs like .info, .tv, .museum... there are loads! You'd be best just using [a-z]{2,6} (as far as I know, no TLDs use numbers or other characters). Commented Dec 13, 2010 at 12:07
  • @Nathan, there are a number of internationalized TLDs which include non-alpha characters: en.wikipedia.org/wiki/… Commented Dec 14, 2010 at 5:51

3 Answers 3

2

UPDATE. Note this is not foolproof by any means.

^([a-zA-Z0-9\-]+\.)+(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$

The + tells the regex to match one or more times

Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't take into account country codes, and the many other top-level domains.
I'm modifying the OPs regex to meet their stated requirements.
You are almost on to something. The first \. doesn't belong.
@Marcelo True, hadn't spotted that. It shouldn't have been there in the first place :)
0

For Perl-compatible regex syntax, you can do this:

^([a-zA-Z0-9-]|\.(?!\.))+\.(com|...

Also, this is very US-centric. My company's domain ends in .com.au.

Comments

0

Why don't you use +? [\.]+ could be matched with one more .!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.