regular expression question?

L

Leon

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
 
T

tom.pesterDeLeTeTHISSS

The regex below did work for me. Hope this helps..

Dim re As New Regex("(?!www\.)([^\s]*?)\.domain\.com")


Dim matches As MatchCollection = re.Matches("zerzaerza rr.domain.com
sfdsqf pp.domain.com ")
Dim match As Match

For Each match In matches
Response.Write("-" & match.Groups(1).ToString)
Next


Cheers,
Tom Pester
 
L

Leon

what can i do to the following to make work the following way?

the user subdomain = tom.domain.com

Current Context.Items("Test") Results =
/Testing/Test1.aspx?ConsultantId=tom/default.aspx

Desired Context.Items("Test") Results =
/Testing/Test1.aspx?ConsultantId=tom

how?

*My Current Code....
Dim subdomain As String = Request.Url.ToString

Dim sdm As Regex

sdm = New Regex ("http://(?!www\.)(.*)(\.vipcarsales\.com)")

Context.Items("Test") = (sdm.Replace(subdomain ,
"/Testing/Test1.aspx?ConsultantId=$1"))



The regex below did work for me. Hope this helps..

Dim re As New Regex("(?!www\.)([^\s]*?)\.domain\.com")
Dim matches As MatchCollection = re.Matches("zerzaerza
rr.domain.com sfdsqf pp.domain.com ")
Dim match As Match
For Each match In matches
Response.Write("-" & match.Groups(1).ToString)
Next


Cheers,
Tom Pester
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
 
G

Guest

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!
***************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top