Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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???????

Posted

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.

Posted
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.

Posted (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 by John_0025
Posted
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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...