Regular expression question?

L

Leon

My web application allow the user to enter the site by typing in a subdomain
such as 'name.domain.com'. However, I want to retrieve just the 'name' part
of the subdomain. see code below (the equal sign '=' respresent the value
I'm getting when I run the code)



Test 1.........................name.domain.com
Dim sdmlink As String = Request.Url.ToString

Dim sdm As Regex

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

Context.Items("Test") = (sdm.Replace(sdmlink, "$1")) ---->> =
http://name.doman.com/welcome.aspx ---- this should = name



Test 2................name.domain.com
Dim sdmlink As String = Request.Url.ToString

Dim sdm As Regex

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

Context.Items("Test") = (sdm.Replace(sdmlink,
"test/default.aspx?Id=$1")) ---->> = test/default.aspx?Id=name ---- this is
correct
 
T

tom.pesterDeLeTeTHISSS

I think you are realy strugling with this. Its best to keep it to the same
post and not post each question seperatly.

Context.Items("Test") = (sdm.Replace(sdmlink, "$1")) ---->> = http://name.doman.com/welcome.aspx
---- this should = name

You've got a spelling mistake :

name.domAn.com should be name.domAIn.com
^ ^^

if its domAIn the result is correct : name

Let me know if you have any more questions..

Cheers,
Tom Pester
 
L

Leon

Yes, I'm struggling, but....
No it's not the spelling. I think the regex expression is not recognizing
the url. what do you think?
I ready do appreciate all the help. Thanks
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)

' Fires at the beginning of each request

Dim sdmurl As String = Request.Url.ToString

Dim sdm As Regex

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

Context.Items("Test") = (sdm.Replace(sdmurl, "/welcome.aspx?Id=$1"))

End Sub
 
T

tom.pesterDeLeTeTHISSS

Hi Leon,

I ran the code below and added the ouput as comments

Dim sdmlink As String = "http://name.domain.com/welcome.aspx"
Dim sdm As New Regex("http://(?!www\.)(.*)(\.domain\.com.*)")
Response.Write("result : " & sdm.Replace(sdmlink, "test/default.aspx?Id=$1")
& "<BR>")
' result : test/default.aspx?Id=name

The regex correctly extracts "name" and the code produces "test/default.aspx?Id=name".
This is what you want no?

I think you are tring to say that there are certain inputs that dont give
the correct result. Otherwise I dont understand your problem.

The input above is

Dim sdmlink As String = "http://name.domain.com/welcome.aspx"

What kind of inputs dont work for you?


Cheers,
Tom Pester
 
L

Leon

What kind of inputs don't work for you?
---> you hit the correct input on the head
http://name.domain.com/welcome.aspx -or- http://name.domain.com/

result : test/default.aspx?Id=name
The regex correctly extracts "name" and the code produces
"test/default.aspx?Id=name".
---> perfect that is the exact results I'm looking for almost, but..

This is what you want no?
--->how do I extract just 'name' -not- 'test/default.aspx?Id=name'

Thanks alot tom!
 
T

tom.pesterDeLeTeTHISSS

--->how do I extract just 'name' -not- 'test/default.aspx?Id=name'

Strange that you ask this because you yourself add it in the code :

Dim sdmlink As String = "http://name.domain.com/welcome.aspx"
Dim sdm As New Regex("http://(?!www\.)(.*)(\.domain\.com.*)")
Response.Write("result : " & sdm.Replace(sdmlink, "test/default.aspx?Id=$1")
& "<BR>")
' result : test/default.aspx?Id=name

your replace with : "test/default.aspx?Id=$1"

if you just want "name" do this :

Dim sdmlink As String = "http://name.domain.com/welcome.aspx"
Dim sdm As New Regex("http://(?!www\.)(.*)(\.domain\.com.*)")
Response.Write("result : " & sdm.Replace(sdmlink, "$1") & "<BR>") '
' result : name

But we already posted this.

I cleaner way to get the "name" is like this :

Dim sdmlink As String = "http://name.domain.com/welcome.aspx"
Dim sdm As New Regex("http://(?!www\.)(.*)(\.domain\.com.*)")
Response.Write(sdm.Match(sdmlink).Groups(1))
' result : name

This produces only "name"

I added you to my msn cause I think your english isnt conversation quality
(no offence :)
If you still can't continue lets have a chat
 
L

Leon

oh I see the problem, but don't know exactly how to fix it...see below

the follow code runs perfect when you pre include the url see... ---->>
http://name.domain.com/welcome.aspx
Dim sdmlink As String = "http://name.domain.com/welcome.aspx"
Dim sdm As New Regex("http://(?!www\.)(.*)(\.domain\.com.*)")
Response.Write("result : " & sdm.Replace(sdmlink, "$1")
' result : name

but what if you get the url in realtime request.url.tostring ---->
request.url.tostring
Dim sdmlink As String = request.url.tostring
Dim sdm As New Regex("http://(?!www\.)(.*)(\.domain\.com.*)")
Response.Write("result : " & sdm.Replace(sdmlink, "$1")
' result : the requested url

I added you to my msn cause I think your english isnt conversation quality
no offence :)
*Cool I don't see that you have added me. No offence taken :)
 
L

Leon

I got it thanks!

Leon said:
oh I see the problem, but don't know exactly how to fix it...see below

the follow code runs perfect when you pre include the url see... ---->>
http://name.domain.com/welcome.aspx
Dim sdmlink As String = "http://name.domain.com/welcome.aspx"
Dim sdm As New Regex("http://(?!www\.)(.*)(\.domain\.com.*)")
Response.Write("result : " & sdm.Replace(sdmlink, "$1")
' result : name

but what if you get the url in realtime request.url.tostring ---->
request.url.tostring
Dim sdmlink As String = request.url.tostring
Dim sdm As New Regex("http://(?!www\.)(.*)(\.domain\.com.*)")
Response.Write("result : " & sdm.Replace(sdmlink, "$1")
' result : the requested url

I added you to my msn cause I think your english isnt conversation quality
no offence :)
*Cool I don't see that you have added me. No offence taken :)
 
T

tom.pesterDeLeTeTHISSS

And what was the problem? Rrealtime Request.url.tostring was not in the expected
format?

Cheers,
Tom Pester

PS Always isolate the poblem to the minimum before giving up on an anwser
yourself.
This means testing the problem code with your own input that you can control
easily.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top