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
PS
This is a repost. But I marked the other post (in .language.vb) as "IGNORE
do not reply here".
...
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".
...