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

Tech-Archive recommends: Fix windows errors by optimizing your registry



On Mar 27, 11:13 am, "sloan" <s...@xxxxxxxxx> wrote:
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

PS
This is a repost. But I marked the other post (in .language.vb) as "IGNORE
do not reply here".

..


Why not just use the Path class in the System.IO namespace?

string filename = @"c:\wutemp\myfile.txt";
string replaceValue = @"c:\newfolder";
string newFileName = Path.Combine(replaceValue,
Path.GetFileName(filename));

Chris

.



Relevant Pages

  • Re: Trouble with getting latest file
    ... Dim FolderName As String ... Dim FileName As String ... If CurrentDateStamp> LatestDateStamp Then ...
    (microsoft.public.access.modulesdaovba)
  • Re: Trouble with getting latest file
    ... I also need to check that I will get the filename of the file with the ... Dim The_FileName As String ... If CurrentDateStamp> LatestDateStamp Then ...
    (microsoft.public.access.modulesdaovba)
  • Need Help with Inserting in SQL
    ... Public logoTable As String ... Dim logoTable As String ... Function InsertLogoToDataBase(ByVal FileName As String) As Variant ... Function GetAllFilesInDir(ByVal strDirPath As String) As Variant ...
    (microsoft.public.excel.programming)
  • Re: Dateidownload per WebRequest (async)
    ... ByVal DestinationDirectory As String, _ ... Dim Response As WebResponse = Request.GetResponse ... If ContentDisposition IsNot Nothing Then ... If Param Is "filename" Then ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • RE: open files in loop with date order
    ... I then tried extracting the filename to left of the dot and saving as ... FileCount = FileCount + 1 ... Dim strPath As String ...
    (microsoft.public.excel.misc)