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)$
ppp
  • 303
  • 2
  • 9
  • 22
  • 1
    Which language/regex facility? – Marcelo Cantos Dec 13 '10 at 11:50
  • 1
    You should change `web url` to `domain name` – Ruel Dec 13 '10 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). – jwueller Dec 13 '10 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). – Nathan MacInnes Dec 13 '10 at 12:07
  • @Nathan, there are a number of internationalized TLDs which include non-alpha characters: http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains#Internationalized_country_code_top-level_domains – eyelidlessness Dec 14 '10 at 05:51
  • @eyelidlessness, Oh yes, they came in recently didn't they. Well I have no idea how you check for them! – Nathan MacInnes Dec 14 '10 at 08:37
  • @Nathan, you check for valid IRIs according to RFC 3987: http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url/190405#190405 – eyelidlessness Dec 14 '10 at 16:55

3 Answers3

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

El Ronnoco
  • 11,753
  • 5
  • 38
  • 65
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.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
0

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

codevania
  • 475
  • 1
  • 5
  • 13