a regex question. help needed

FlyBoy

Well-known member
Joined
Sep 6, 2004
Messages
106
i have the following pattern:
([\w\-]+\.)+
([\w\-]{2,3})

which should validate a hostname adress.
i cand understan some parts of it.
i know that the \w represents and "word" chaaracter.
but what the \- does???????
 
Its a hyphen - so matches the hyphen in Xtreme-talk.com

In regular expressions the hyphen is used to show a range of allowed characters. i.e. 0-8 (matchs 0,1,2,3,4,5,6,7,8) so you have to show that you want to match a hyphen and you are not using it as a special character therefor you need to put the \ symbol in from.

[A-Z] matchs any letter but [e\-t] matchs the hyphen and will find matchs with e-t in only. like in Xtreme-talk.
Hope this makes sense.
 
John_0025 said:
Its a hyphen - so matches the hyphen in Xtreme-talk.com

In regular expressions the hyphen is used to show a range of allowed characters. i.e. 0-8 (matchs 0,1,2,3,4,5,6,7,8) so you have to show that you want to match a hyphen and you are not using it as a special character therefor you need to put the \ symbol in from.

[A-Z] matchs any letter but [e\-t] matchs the hyphen and will find matchs with e-t in only. like in Xtreme-talk.
Hope this makes sense.


10x for the reply!
this pattern as i said before matches to host names such as : www.contoso.com


i dont see any hyphen in here. :( :( still cant understand this hyphen thingy.

if i had to rewrite this pattern i would do it like this:
(\w{3}\.)+(\w (2,3)

and i guess its wrong.
 
Some domain name do have the hyphen in and the expression you were using was to try and match all of them.

If you are sure there will never be a hyphen in the name you are trying to match then you can miss it out.

Code:
([\w]+\.)+([\w]{2,3})

which will match www.contoso.com but it wont match a website called www.j-walk.com. Your original pattern would.

I see what you are trying to do with your pattern. Maybe it might help you to start of with a simple pattern that will match your example. Then see how you would change it to deal with other host names.

Try

www.[a-z]{1,}.[a-z]{2,3}
 
Last edited by a moderator:
John_0025 said:
Some domain name do have the hyphen in and the expression you were using was to try and match all of them.

If you are sure there will never be a hyphen in the name you are trying to match then you can miss it out.

PHP:
([\w]+\.)+([\w]{2,3})

which will match www.contoso.com but it wont match a website called www.j-walk.com. Your original pattern would.

I see what you are trying to do with your pattern. Maybe it might help you to start of with a simple pattern that will match your example. Then see how you would change it to deal with other host names.

Try

www.[a-z]{1,}.[a-z]{2,3}


10x mate! now i uderstand it! ! ! many 10x
 
Back
Top