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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sloan

    #1

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



    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.

    "Unrecogniz ed 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\myfi le.txt" '<< this is given to me, I cannot simply
    say " filename = "C:\\wutemp\\my file.txt" "

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

    Dim findValue As String = "\wutemp\"

    Dim replaceValue As String = "\newfolder \"

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







    Private Function GetRegexOptions () As RegexOptions



    Dim options As RegexOptions = New RegexOptions

    options = options Or RegexOptions.Ig noreCase

    Return options

    End Function


  • sloan

    #2
    Re: RegEx and Vb.net /// &quot;Unrecogni zed escape sequence&quot;


    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.


    ..........




    "sloan" <sloan@ipass.ne twrote in message
    news:OYhfTCIcHH A.2552@TK2MSFTN GP06.phx.gbl...
    >
    >
    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.
    >
    "Unrecogniz ed 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\myfi le.txt" '<< this is given to me, I cannot
    simply
    say " filename = "C:\\wutemp\\my file.txt" "
    >
    Dim replaceRegEx As System.Text.Reg ularExpressions .Regex = New
    System.Text.Reg ularExpressions .Regex(fileName , getRegexOptions ())
    >
    Dim findValue As String = "\wutemp\"
    >
    Dim replaceValue As String = "\newfolder \"
    >
    Dim newFileName As String = replaceRegEx.Re place(fileName, findValue,
    replaceValue)
    >
    >
    >
    >
    >
    >
    >
    Private Function GetRegexOptions () As RegexOptions
    >
    >
    >
    Dim options As RegexOptions = New RegexOptions
    >
    options = options Or RegexOptions.Ig noreCase
    >
    Return options
    >
    End Function
    >
    >

    Comment

    • sloan

      #3
      Re: RegEx and Vb.net /// &quot;Unrecogni zed escape sequence&quot;


      I'm moving this post to:
      microsoft.publi c.dotnet.framew ork.aspnet

      Please IGNORE this post/thread and reply there.





      "sloan" <sloan@ipass.ne twrote in message
      news:OwH2QGIcHH A.2120@TK2MSFTN GP03.phx.gbl...
      >
      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.
      >
      >
      .........
      >
      >
      >
      >
      "sloan" <sloan@ipass.ne twrote in message
      news:OYhfTCIcHH A.2552@TK2MSFTN GP06.phx.gbl...


      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.

      "Unrecogniz ed 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\myfi le.txt" '<< this is given to me, I cannot
      simply
      say " filename = "C:\\wutemp\\my file.txt" "

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

      Dim findValue As String = "\wutemp\"

      Dim replaceValue As String = "\newfolder \"

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







      Private Function GetRegexOptions () As RegexOptions



      Dim options As RegexOptions = New RegexOptions

      options = options Or RegexOptions.Ig noreCase

      Return options

      End Function
      >
      >

      Comment

      • Chris Dunaway

        #4
        Re: RegEx and Vb.net /// &quot;Unrecogni zed escape sequence&quot;

        On Mar 27, 10:06 am, "sloan" <s...@ipass.net wrote:
        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.
        >
        .........
        >
        "sloan" <s...@ipass.net wrote in message
        >
        news:OYhfTCIcHH A.2552@TK2MSFTN GP06.phx.gbl...
        >
        >
        >
        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.
        >
        "Unrecogniz ed 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\myfi le.txt" '<< this is given to me, I cannot
        simply
        say " filename = "C:\\wutemp\\my file.txt" "
        >
        Dim replaceRegEx As System.Text.Reg ularExpressions .Regex = New
        System.Text.Reg ularExpressions .Regex(fileName , getRegexOptions ())
        >
        Dim findValue As String = "\wutemp\"
        >
        Dim replaceValue As String = "\newfolder \"
        >
        Dim newFileName As String = replaceRegEx.Re place(fileName, findValue,
        replaceValue)
        >
        Private Function GetRegexOptions () As RegexOptions
        >
        Dim options As RegexOptions = New RegexOptions
        >
        options = options Or RegexOptions.Ig noreCase
        >
        Return options
        >
        End Function
        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\myfi le.txt"
        Dim replaceValue As String = "c:\newFold er"
        Dim newFilename As String = Path.Combine(re placeValue,
        Path.GetFileNam e(filename))

        Chris


        Comment

        • sloan

          #5
          Re: RegEx and Vb.net /// &quot;Unrecogni zed escape sequence&quot;


          Man , I can't think straight today.

          I meant to say it will be moved to:

          microsoft.publi c.dotnet.framew ork











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



          "sloan" <sloan@ipass.ne twrote in message
          news:%2388xCpIc HHA.1296@TK2MSF TNGP02.phx.gbl. ..
          >
          I'm moving this post to:
          microsoft.publi c.dotnet.framew ork.aspnet
          >
          Please IGNORE this post/thread and reply there.
          >
          >
          >
          >
          >
          "sloan" <sloan@ipass.ne twrote in message
          news:OwH2QGIcHH A.2120@TK2MSFTN GP03.phx.gbl...

          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.


          .........




          "sloan" <sloan@ipass.ne twrote in message
          news:OYhfTCIcHH A.2552@TK2MSFTN GP06.phx.gbl...
          >
          >
          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.
          >
          "Unrecogniz ed 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\myfi le.txt" '<< this is given to me, I cannot
          simply
          say " filename = "C:\\wutemp\\my file.txt" "
          >
          Dim replaceRegEx As System.Text.Reg ularExpressions .Regex = New
          System.Text.Reg ularExpressions .Regex(fileName , getRegexOptions ())
          >
          Dim findValue As String = "\wutemp\"
          >
          Dim replaceValue As String = "\newfolder \"
          >
          Dim newFileName As String = replaceRegEx.Re place(fileName, findValue,
          replaceValue)
          >
          >
          >
          >
          >
          >
          >
          Private Function GetRegexOptions () As RegexOptions
          >
          >
          >
          Dim options As RegexOptions = New RegexOptions
          >
          options = options Or RegexOptions.Ig noreCase
          >
          Return options
          >
          End Function
          >
          >
          >
          >

          Comment

          • Newbie Coder

            #6
            Re: RegEx and Vb.net /// &quot;Unrecogni zed escape sequence&quot;

            Sloan,

            Why cant you just do String.Replace( "\\", "\")?

            --
            Newbie Coder
            (It's just a name)


            "sloan" <sloan@ipass.ne twrote in message
            news:OYhfTCIcHH A.2552@TK2MSFTN GP06.phx.gbl...
            >
            >
            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.
            >
            "Unrecogniz ed 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\myfi le.txt" '<< this is given to me, I cannot
            simply
            say " filename = "C:\\wutemp\\my file.txt" "
            >
            Dim replaceRegEx As System.Text.Reg ularExpressions .Regex = New
            System.Text.Reg ularExpressions .Regex(fileName , getRegexOptions ())
            >
            Dim findValue As String = "\wutemp\"
            >
            Dim replaceValue As String = "\newfolder \"
            >
            Dim newFileName As String = replaceRegEx.Re place(fileName, findValue,
            replaceValue)
            >
            >
            >
            >
            >
            >
            >
            Private Function GetRegexOptions () As RegexOptions
            >
            >
            >
            Dim options As RegexOptions = New RegexOptions
            >
            options = options Or RegexOptions.Ig noreCase
            >
            Return options
            >
            End Function
            >
            >

            Comment

            • sloan

              #7
              Re: RegEx and Vb.net /// &quot;Unrecogni zed escape sequence&quot;




              I'm moving this post to:
              microsoft.publi c.dotnet.framew ork

              Please IGNORE this post/thread and reply there.





              "Newbie Coder" <newbiecoder@sp ammeplease.comw rote in message
              news:eloSFzIcHH A.1300@TK2MSFTN GP02.phx.gbl...
              Sloan,
              >
              Why cant you just do String.Replace( "\\", "\")?
              >
              --
              Newbie Coder
              (It's just a name)
              >
              >
              "sloan" <sloan@ipass.ne twrote in message
              news:OYhfTCIcHH A.2552@TK2MSFTN GP06.phx.gbl...


              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.

              "Unrecogniz ed 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\myfi le.txt" '<< this is given to me, I cannot
              simply
              say " filename = "C:\\wutemp\\my file.txt" "

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

              Dim findValue As String = "\wutemp\"

              Dim replaceValue As String = "\newfolder \"

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







              Private Function GetRegexOptions () As RegexOptions



              Dim options As RegexOptions = New RegexOptions

              options = options Or RegexOptions.Ig noreCase

              Return options

              End Function
              >
              >

              Comment

              Working...