maztt
maztt

Reputation: 12294

Match domain names as regular expressions

can some tell how can i write regular expression matching abc.com.ae or abc.net.af or anything ,ae which is the last in the string is optional

this is successfull with / but not . don't know why

^[a-z]{1,25}.[a-z]{3}$

answer

^[a-z]{1,30}\.[a-z]{3}((\.)[a-z]{2})?$

Upvotes: 1

Views: 1166

Answers (3)

Contango
Contango

Reputation: 80282

I sounds like you need some environment to learn regular expressions.

The best way to learn regular expressions is with an interactive regular expression simulator: type in an expression, and it will give you the results, instantly.

There are a few free (and not so free) regular expression simulators available:

Upvotes: 0

Timwi
Timwi

Reputation: 66583

The answer is: write \. instead of ., because . means “any character”.

Upvotes: 4

Nobody
Nobody

Reputation: 4841

You can match specific characters using the \u escape code followed immediately by the unicode number for that character in hex format and it must also be four digits only. I think a full stop is \u002E.

In your example where '/' works but not '.', this is because a '/' is recognised as a normal character and does not need to be escaped whereas the full stop has a different meaning in c# regex (it matches any character).

If youre still not sure, here is a useful guide to regex in C#: http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx And this one has examples of regex for URLs: http://www.radsoftware.com.au/articles/regexsyntaxadvanced.aspx

Upvotes: 0

Related Questions