Raised This Month: $12 Target: $400
 3% 

Regex pattern for hostnames


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 03-14-2012 , 18:37   Regex pattern for hostnames
Reply With Quote #1

Hi. I tried several patters hoping some of them will work, but none worked properly.
cs.example.com
www.test.net
etc..

PHP Code:
"(?:(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]{0,61})?[a-zA-Z0-9])[.])*(?:[a-zA-Z][-a-zA-Z0-9]{0,61}[a-zA-Z0-9]|[a-zA-Z])[.]?)" 
PHP Code:
"[a-zA-Z0-9]+([a-zA-Z0-9\-\.]+)?\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$" 
PHP Code:
"([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$" 
any help? thanks
__________________

anakin_cstrike is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 03-14-2012 , 19:13   Re: Regex pattern for hostnames
Reply With Quote #2

([a-z0-9\-]+\.)+[a-z]{2,4}

Be sure it is case-insensitive.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 03-14-2012 at 19:14.
Exolent[jNr] is offline
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 03-14-2012 , 19:27   Re: Regex pattern for hostnames
Reply With Quote #3

works, thanks
but how about when i write "www.google. com" or "www. address . com" i mean, those spaces/blanks

EDIT: ok i tried something and seems to work xD
PHP Code:
([a-z0-9\-]+\.|[a-z0-9\-]+\ )+[a-z]{2,4
but even if i write "ssssss.test" or "hahaha.go" it says it matches..

EDIT2:
finally got something like this, at first look seems alright but there is still the problem with the spaces..
PHP Code:
([a-z0-9\-]+\.|[a-z0-9\-]+\ )+(com|org|net|mil|info|edu|COM|ORG|NET|MIL|EDU|INFO
__________________


Last edited by anakin_cstrike; 03-14-2012 at 19:55.
anakin_cstrike is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 03-14-2012 , 21:10   Re: Regex pattern for hostnames
Reply With Quote #4

There's a lot more than those domain extensions, but anyway.

^(\w*\W+)?([\w\-\s]+\.)+\s*(c\s*o\s*m|o\s*r\s*g|n\s*e\s*t|m\s*i\ s*l|i\s*n\s*f\s*o|e\s*d\s*u)(\W+\w*)?$
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 03-14-2012 , 21:13   Re: Regex pattern for hostnames
Reply With Quote #5

doesn't work
www google com
doesn't match
__________________

anakin_cstrike is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 03-14-2012 , 21:23   Re: Regex pattern for hostnames
Reply With Quote #6

Quote:
Originally Posted by anakin_cstrike View Post
doesn't work
www google com
doesn't match
Because there's no periods.

Try this if you don't want to require periods.

^(\w*\W+)?([\w\-\s]+\.?)+\s*(c\s*o\s*m|o\s*r\s*g|n\s*e\s*t|m\s*i \ s*l|i\s*n\s*f\s*o|e\s*d\s*u)(\W+\w*)?$
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 03-14-2012 , 22:58   Re: Regex pattern for hostnames
Reply With Quote #7

Seems to work fine, thanks
How can i add more subdomains like ".ro" or ".info" ?
also is there any chance on getting that optimizated? i mean if i write "word word word word net" it matches but i only want to block saying hostnames and such
__________________

anakin_cstrike is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 03-15-2012 , 00:19   Re: Regex pattern for hostnames
Reply With Quote #8

Quote:
Originally Posted by anakin_cstrike View Post
Seems to work fine, thanks
How can i add more subdomains like ".ro" or ".info" ?
also is there any chance on getting that optimizated? i mean if i write "word word word word net" it matches but i only want to block saying hostnames and such
There is no good way to do what you want, IMO, because a url nearly limitless and like you stated above, the periods can be omitted and a human will understand but the computer will not.
__________________
fysiks is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 00:55.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode