FlyBoy Posted September 6, 2004 Posted September 6, 2004 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??????? Quote
John_0025 Posted September 6, 2004 Posted September 6, 2004 It's 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. Quote
FlyBoy Posted September 6, 2004 Author Posted September 6, 2004 It's 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 : http://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. Quote
John_0025 Posted September 7, 2004 Posted September 7, 2004 (edited) 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. ([\w]+\.)+([\w]{2,3}) which will match 'www.contoso.com' but it won't 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} Edited September 7, 2004 by John_0025 Quote
FlyBoy Posted September 7, 2004 Author Posted September 7, 2004 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. ([\w]+\.)+([\w]{2,3}) which will match 'www.contoso.com' but it won't 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.