Regular Expressions .NET

  • Thread starter Thread starter Justin Fancy
  • Start date Start date
J

Justin Fancy

I am new to regular expressions with .net. I was wondering if someone
could help me out.

I'm looking for the lines of code that will replace C: with
nothing(""), AND all backslashes"\" to forward slashes"/".

Example:

ORGINAL

x:\en\aviation\users.htm

FINISHED PRODUCT

/en/aviation/users.htm

It would be awesome if I could hear from someone on this matter.

Thanks!

Justin
 
You mean something like:

Dim str As String = "C:\en\aviation\users.htm"
str = Regex.Replace(str, "C:", "")
str = Regex.Replace(str, "\\", "/")
MsgBox(str)

Note, the following does the same thing:

Dim str As String = "C:\en\aviation\users.htm"
str.Replace("C:", "")
str.Replace("\\", "/")
MsgBox(str)

Thanks,

Seth Rowe
 
Note, the following does the same thing:
Note, the following does the same thing:

Dim str As String = "C:\en\aviation\users.htm"
str.Replace("C:", "")
str.Replace("\\", "/")
MsgBox(str)

Oops, change "\\" to "\" Making it:

Dim str As String = "C:\en\aviation\users.htm"
str.Replace("C:", "")
str.Replace("\", "/")
MsgBox(str)

Using Regex.Replace requires using double backslashes as a backslash
has special meaning to Regex. This cause str.Replace("\\", "/") to look
for 2 backslashes instead of just one.

Thanks,

Seth Rowe
 
Justin said:
I am new to regular expressions with .net. I was wondering if someone
could help me out.

I'm looking for the lines of code that will replace C: with
nothing(""), AND all backslashes"\" to forward slashes"/".

Example:

ORGINAL

x:\en\aviation\users.htm

FINISHED PRODUCT

/en/aviation/users.htm

It would be awesome if I could hear from someone on this matter.

Thanks!

Justin

Removing the drive letter can be done with a regular expression:

path = Regex.Replace(path, "^[A-Z]:", "")

Replacing the backslashes can be done with just a regular replace.
 
Thanks a million people!!

J
Göran Andersson said:
Justin said:
I am new to regular expressions with .net. I was wondering if someone
could help me out.

I'm looking for the lines of code that will replace C: with
nothing(""), AND all backslashes"\" to forward slashes"/".

Example:

ORGINAL

x:\en\aviation\users.htm

FINISHED PRODUCT

/en/aviation/users.htm

It would be awesome if I could hear from someone on this matter.

Thanks!

Justin

Removing the drive letter can be done with a regular expression:

path = Regex.Replace(path, "^[A-Z]:", "")

Replacing the backslashes can be done with just a regular replace.
 
Thanks for the correction Jay!

Perhaps I should type my responses in the ide before posting
them.......

:-)

Thanks,

Seth Rowe
 
Back
Top