RegEx and Vb.net /// "Unrecognized escape sequence"

S

sloan

I have a fairly simple RegEx code below.

I am given a file name, (which I don't control) , and need to change a
folder name in it.



The code below is choking on the filename not being escaped.

"Unrecognized escape sequence"

While I can escape the findValue and replaceValue,
I don't necessarily control the fileName value. Aka, all I can do is
manually string.Replace the fileName value. (Unless someone knows better
than I)

Do I have to do a string.Replace here? ( to make all the \ into \\ )

Or am I missing some trick in vb.net.



----------Start VB.Net code



Dim fileName As String

fileName = "C:\wutemp\myfile.txt" '<< this is given to me, I cannot simply
say " filename = "C:\\wutemp\\myfile.txt" "

Dim replaceRegEx As System.Text.RegularExpressions.Regex = New
System.Text.RegularExpressions.Regex(fileName, getRegexOptions())

Dim findValue As String = "\wutemp\"

Dim replaceValue As String = "\newfolder\"

Dim newFileName As String = replaceRegEx.Replace(fileName, findValue,
replaceValue)







Private Function GetRegexOptions() As RegexOptions



Dim options As RegexOptions = New RegexOptions

options = options Or RegexOptions.IgnoreCase

Return options

End Function
 
S

sloan

PS

Even when I write code that doesn't "break it", I get a value like

newFileName
"C:\\\newfolder\\\myfile.txt"

Which isn't what I want of course.


..........
 
S

sloan

I'm moving this post to:
microsoft.public.dotnet.framework.aspnet

Please IGNORE this post/thread and reply there.
 
C

Chris Dunaway

PS

Even when I write code that doesn't "break it", I get a value like

newFileName
"C:\\\newfolder\\\myfile.txt"

Which isn't what I want of course.

.........

I'm not sure why you're using double slashes in your path names as
VB.Net does not use the \ character as an escape character in strings.

Why not use the classes in the System.IO namespace?

Dim filename As String = "c:\wutemp\myfile.txt"
Dim replaceValue As String = "c:\newFolder"
Dim newFilename As String = Path.Combine(replaceValue,
Path.GetFileName(filename))

Chris
 
S

sloan

I'm moving this post to:
microsoft.public.dotnet.framework

Please IGNORE this post/thread and reply there.
 
S

sloan

Man , I can't think straight today.

I meant to say it will be moved to:

microsoft.public.dotnet.framework











string t6 =
@"C:\Documents and Settings\user1\Desktop\";
string r6 = Regex.Replace(t6,
@"\\user1\\",
@"\\user2\\");
 

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

Top