0

I need to create a regex that identify a string if that is a url with my criteria but i got stuck in identifying the domain name...

the criteria for domain name is

  1. only [a-z][0-9], . and -
  2. first char must be [a-z]
  3. before and after . or - must be [a-z]
  4. min length 1 char

and the domain region is

  1. only [a-z] and .
  2. first char must be .
  3. min length 3 char

format should be www.<domain name>.<domain region>

here what i got..

www.[a-z]([a-z0-9\.\-]{1,}[a-z][\.\-])+[^\.+\-].[a-z+\.]{2,}[^\.]$

this is for the domain name

[a-z]([a-z0-9\.\-]{1,}[a-z][\.\-])+[^\.+\-]

this is for the domain region

.[a-z+\.]{2,}[^\.]$

this will be www.sample.com.sg

  • www..sample.com will be rejected because of .sample
  • www.sample..com will be rejected because of sample.
  • www.sample.com.sg. will be rejected because of .com.sg.
  • www.sampl3.sample.com will be rejected because of sampl3. rules numb 3.
5
  • here is a start: www\.[a-z]([.-][a-z]|[a-z0-9])+ Commented Sep 24, 2012 at 13:45
  • are you sure about rule 3? What is wrong with all-4-one.com? Commented Sep 24, 2012 at 13:46
  • thx.. 4 ur fast response.. but at the end of domain name, it can't be a '.' or '-'.. how can i validate that? err.. i don't think this is a great criteria for identifying a url, but i got a assignment and this is the criteria my lecturer give.. so thats why..ahahahhaa..xD Commented Sep 24, 2012 at 13:48
  • it is only a start. It should take you up to the TLD - but not with your extra rule about only letters around hyphens. Commented Sep 24, 2012 at 14:16
  • hahaha.. yes.. that's true.. with this extra rules this can't be TLD..@_@ Commented Sep 24, 2012 at 14:21

3 Answers 3

3

Reinvent not

Things like this have been done so many times, it's better to leverage the existing code than to relearn all the URL rules and spec requirements.

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

1 Comment

yes.. that's will be a great way to identify the url in easier way..but i have to go with the regex.. so thx for answering..:D
0

Try this one out: ^[a-zA-Z0-9-.]+.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$ I haven't pretested it, so few regulations may be necessary

3 Comments

hmm.. i've seen that code before.. but i have to go with that criteria i described above..anyway thanks for answering..:D
com/org etc fails to match anything outside the US; .eu, .co.uk, .it. It will even fail with common things like .me, bit.ly etc.
yes.. and that's only for the domain name that u mentioned (com|org|net|mil|edu|COM|ORG|NET|MIL|EDU).. other than that will fail
0

EDIT:

www\.(([a-z][a-z0-9]*)?[a-z][\.\-])*([a-z][a-z0-9]*)?[a-z]\.[a-z]{2,}$

Crazy long so maybe there's better ones but...

6 Comments

Yeah i missed the that the domain name must start with [a-z] but was it more than that? I'll edit the regExp.
Missed the [a-z] before and after [\.\-] part to
ahahha... yes.. and the last of domain name cannot be a . or -.. and there cant be numbers before . and -..@_@
what if it's like this [a-z]([a-z]+[0-9]*[a-z]+|[\.\-])+
i dont really get what ? means??.. @@
|

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.