I find it easier to start with two question:
1. Is it an URL
2. Does it start with www
The answer is to figure out first if it is an url and then exclude if it has
a match on www. The following is not a final answer, but it will answer the
question you are looking to answer:
//C# version
private static bool IsNonWwwUrl(string url)
{
Regex wwwRegex = new Regex(
@"([!www]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
Regex urlRegex = new Regex(
@"([\w-]+\.)+[\\w-]+(/[\w- ./?%&=]*)?");
Return ((urlRegex.IsMatch(url)&&
(!wwwRegex.IsMatch(url));
}
'VB.NET answer
Function IsNonWwwUrl(ByRef url As String) As Boolean
'this one should not match
Dim wwwRegex As New Regex(_
"([!www]+\.)+[\w-]+(/[\w- ./?%&=]*)?")
'this one should match
Dim urlRegex As New Regex(_
"([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?")
Return ((urlRegex.IsMatch(url)) And _
Not wwwRegex.IsMatch(url)))
End Function
If you want to include the possibility of http:// and https:// you will look
for number of matches rather than match the entire string (this is C# only,
apologies):
private static bool IsNonWwwUrl(string url)
{
Regex wwwRegex = new Regex(
@"([!www]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
Regex urlRegex = new Regex(
@"([\w-]+\.)+[\\w-]+(/[\w- ./?%&=]*)?");
if((wwwRegex.Matches(url) == 0)&&
(urlRegex.Matches(url) != 0))
return true;
else
return false;
}
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
***************************
Think Outside the Box!
***************************
"Leon" wrote:
why is the following not correct in asp.net? I'm trying to match all
subdomain names 'leon.domain.com', but
not 'www.domain.com'?
Dim sdm As Regex
sdm = New Regex (?!www\.)(.*)\.domain\.com