Home All Groups Group Topic Archive Search About

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

Author
27 Mar 2007 4:13 PM
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




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

...

Author
27 Mar 2007 4:22 PM
dunawayc
Show quote
On Mar 27, 11:13 am, "sloan" <s***@ipass.net> 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
Author
27 Mar 2007 4:27 PM
sloan
//quote
> 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.
// end quote

That's the issue.  I *don't want* to use the double slashed, and wouldn't
think (since I'm in vb.net on this one , and not c# ) I'd have to use
delimited characters.
But the code breaks .... if I use single slashes.


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


That would be an option, but I actually have more complex rules, that RegEx
solves perfectly.
I just used the "change folder name" as a dumbed down example.



I did all this at home in C# , and had it working.  I brought it into work
and this project was vb.net , and I converted the code.







<dunaw***@gmail.com> wrote in message
Show quote
news:1175012533.361598.190110@b75g2000hsg.googlegroups.com...
> On Mar 27, 11:13 am, "sloan" <s***@ipass.net> 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
>
Author
27 Mar 2007 5:17 PM
Patrice
You'll have to use \\ even when not using C#.

Try http://msdn2.microsoft.com/en-us/library/ae5bf541(VS.80).aspx and note
that \ is the escape characters for regular expressions regardless of the
"host language".


Not that \ is the escape character for regular expressions. So this is not
because you are using VB.NET that you have to change the regular
"sloan" <sl***@ipass.net> a écrit dans le message de news:
OYCvZzIcHHA.1***@TK2MSFTNGP04.phx.gbl...
Show quote
> //quote
>> 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.
> // end quote
>
> That's the issue.  I *don't want* to use the double slashed, and wouldn't
> think (since I'm in vb.net on this one , and not c# ) I'd have to use
> delimited characters.
> But the code breaks .... if I use single slashes.
>
>
> //quote
>> Dim filename As String = "c:\wutemp\myfile.txt"
>> Dim replaceValue As String = "c:\newFolder"
>> Dim newFilename As String = Path.Combine(replaceValue,
>> Path.GetFileName(filename))
> //end quote
>
>
> That would be an option, but I actually have more complex rules, that
> RegEx
> solves perfectly.
> I just used the "change folder name" as a dumbed down example.
>
>
>
> I did all this at home in C# , and had it working.  I brought it into work
> and this project was vb.net , and I converted the code.
>
>
>
>
>
>
>
> <dunaw***@gmail.com> wrote in message
> news:1175012533.361598.190110@b75g2000hsg.googlegroups.com...
>> On Mar 27, 11:13 am, "sloan" <s***@ipass.net> 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
>>
>
>
Author
27 Mar 2007 6:14 PM
sloan
//and note
> that \ is the escape characters for regular expressions regardless of the
> "host language".//

Ahhh....
Thus my error in my C# to VB.net translation.

Thanks Patrice.




Show quote
"Patrice" <http://www.chez.com/scribe/> wrote in message
news:eiN4UPJcHHA.208@TK2MSFTNGP05.phx.gbl...
> You'll have to use \\ even when not using C#.
>
> Try http://msdn2.microsoft.com/en-us/library/ae5bf541(VS.80).aspx and note
> that \ is the escape characters for regular expressions regardless of the
> "host language".
>
>
> Not that \ is the escape character for regular expressions. So this is not
> because you are using VB.NET that you have to change the regular
> "sloan" <sl***@ipass.net> a écrit dans le message de news:
> OYCvZzIcHHA.1***@TK2MSFTNGP04.phx.gbl...
> > //quote
> >> 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.
> > // end quote
> >
> > That's the issue.  I *don't want* to use the double slashed, and
wouldn't
> > think (since I'm in vb.net on this one , and not c# ) I'd have to use
> > delimited characters.
> > But the code breaks .... if I use single slashes.
> >
> >
> > //quote
> >> Dim filename As String = "c:\wutemp\myfile.txt"
> >> Dim replaceValue As String = "c:\newFolder"
> >> Dim newFilename As String = Path.Combine(replaceValue,
> >> Path.GetFileName(filename))
> > //end quote
> >
> >
> > That would be an option, but I actually have more complex rules, that
> > RegEx
> > solves perfectly.
> > I just used the "change folder name" as a dumbed down example.
> >
> >
> >
> > I did all this at home in C# , and had it working.  I brought it into
work
> > and this project was vb.net , and I converted the code.
> >
> >
> >
> >
> >
> >
> >
> > <dunaw***@gmail.com> wrote in message
> > news:1175012533.361598.190110@b75g2000hsg.googlegroups.com...
> >> On Mar 27, 11:13 am, "sloan" <s***@ipass.net> 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
> >>
> >
> >
>
>

AddThis Social Bookmark Button