regular expressions

Discussion in 'ASP .Net' started by Jon Paal, Sep 4, 2006.

  1. Jon   Paal

    Jon Paal Guest

    when reading a string from a file how to clean off "new line" and "end of line" stuff so text is returned as a clean single line ?
    Jon Paal, Sep 4, 2006
    #1
    1. Advertising

  2. Jon   Paal

    Tim_Mac Guest

    you could just use Trim() on the string which should remove any whitespace
    off either end of the line.
    but if you want to use regex...

    \r is carriage return
    \n is newline
    a simple replace on these characters with the empty string will sort it out.
    System.Text.RegularExpressions.Regex.Replace(input, @"\r\n", "");
    hope this helps
    tim



    "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message
    news:%...
    > when reading a string from a file how to clean off "new line" and "end of
    > line" stuff so text is returned as a clean single line ?
    >
    >
    >
    Tim_Mac, Sep 6, 2006
    #2
    1. Advertising

  3. Jon   Paal

    Jon Paal Guest

    thanks for the help. It looked like a promising solution but unfortunately did not solve my problem.

    I'm trying to read a string from a text file so I can encrypt it.

    When I create the text file manually, it reads and encrypts correctly.

    If I try to create the text file dynamically, it encrypts to a slightly different outcome.

    I can't understand why the difference.

    <code>

    Private Shared ReadOnly encryptionKeyBytes() As Byte = New Byte() {53, 53, 53, 53, 53, 53, 53, 53}
    Private Shared Function GenerateEncryptedLicFile(ByVal inputText As String, ByVal outputFile As String) As Boolean

    Dim outputStream As Stream = Nothing
    Dim encryptedStream As Stream = Nothing
    Dim result As Boolean = False

    Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider()
    des.Key = encryptionKeyBytes
    des.IV = encryptionKeyBytes

    Try
    outputStream = New FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)

    Dim desEncryptor As ICryptoTransform = des.CreateEncryptor()
    encryptedStream = New CryptoStream(outputStream, desEncryptor, CryptoStreamMode.Write)

    Dim contents() As Byte = des.EncryptValue(inputText)

    encryptedStream.Write(contents, 0, contents.Length)
    result = True

    Finally
    If Not encryptedStream Is Nothing Then
    encryptedStream.Close()
    encryptedStream = Nothing
    End If
    If Not outputStream Is Nothing Then
    outputStream.Close()
    outputStream = Nothing
    End If
    End Try

    Return result
    End Function

    </code>









    "Tim_Mac" <> wrote in message news:...
    > you could just use Trim() on the string which should remove any whitespace off either end of the line.
    > but if you want to use regex...
    >
    > \r is carriage return
    > \n is newline
    > a simple replace on these characters with the empty string will sort it out.
    > System.Text.RegularExpressions.Regex.Replace(input, @"\r\n", "");
    > hope this helps
    > tim
    >
    >
    >
    > "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message news:%...
    >> when reading a string from a file how to clean off "new line" and "end of line" stuff so text is returned as a clean single line
    >> ?
    >>
    >>
    >>

    >
    >
    Jon Paal, Sep 6, 2006
    #3
  4. Jon   Paal

    Tim_Mac Guest

    hi Jon,
    how do you know which is the right one?
    can you post your decrypt code.
    it could be the \r\n carriage return encoded by notepad if that's how you
    created the text file manually.
    if you debug, the debugger should show you all the whitespace characters so
    you can compare the contents of the file with the string variable, and see
    what is the difference.

    let me know how you get on. by the way, i can't see where this relates to
    the first post, just trying to piece it together!
    tim


    "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message
    news:...
    > thanks for the help. It looked like a promising solution but
    > unfortunately did not solve my problem.
    >
    > I'm trying to read a string from a text file so I can encrypt it.
    >
    > When I create the text file manually, it reads and encrypts correctly.
    >
    > If I try to create the text file dynamically, it encrypts to a slightly
    > different outcome.
    >
    > I can't understand why the difference.
    >
    > <code>
    >
    > Private Shared ReadOnly encryptionKeyBytes() As Byte = New Byte() {53,
    > 53, 53, 53, 53, 53, 53, 53}
    > Private Shared Function GenerateEncryptedLicFile(ByVal inputText As
    > String, ByVal outputFile As String) As Boolean
    >
    > Dim outputStream As Stream = Nothing
    > Dim encryptedStream As Stream = Nothing
    > Dim result As Boolean = False
    >
    > Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider()
    > des.Key = encryptionKeyBytes
    > des.IV = encryptionKeyBytes
    >
    > Try
    > outputStream = New FileStream(outputFile, FileMode.Create,
    > FileAccess.Write, FileShare.None)
    >
    > Dim desEncryptor As ICryptoTransform = des.CreateEncryptor()
    > encryptedStream = New CryptoStream(outputStream, desEncryptor,
    > CryptoStreamMode.Write)
    >
    > Dim contents() As Byte = des.EncryptValue(inputText)
    >
    > encryptedStream.Write(contents, 0, contents.Length)
    > result = True
    >
    > Finally
    > If Not encryptedStream Is Nothing Then
    > encryptedStream.Close()
    > encryptedStream = Nothing
    > End If
    > If Not outputStream Is Nothing Then
    > outputStream.Close()
    > outputStream = Nothing
    > End If
    > End Try
    >
    > Return result
    > End Function
    >
    > </code>
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > "Tim_Mac" <> wrote in message
    > news:...
    >> you could just use Trim() on the string which should remove any
    >> whitespace off either end of the line.
    >> but if you want to use regex...
    >>
    >> \r is carriage return
    >> \n is newline
    >> a simple replace on these characters with the empty string will sort it
    >> out.
    >> System.Text.RegularExpressions.Regex.Replace(input, @"\r\n", "");
    >> hope this helps
    >> tim
    >>
    >>
    >>
    >> "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message
    >> news:%...
    >>> when reading a string from a file how to clean off "new line" and "end
    >>> of line" stuff so text is returned as a clean single line ?
    >>>
    >>>
    >>>

    >>
    >>

    >
    >
    Tim_Mac, Sep 6, 2006
    #4
  5. Jon   Paal

    Jon Paal Guest

    found my problem.

    when writing the text file dynamically, I was using objStreamWriter.WriteLine, instead of objStreamWriter.Write, so it was adding a
    newline to the entry which I could not strip off.


    thanks for responding



    "Tim_Mac" <> wrote in message news:...
    > hi Jon,
    > how do you know which is the right one?
    > can you post your decrypt code.
    > it could be the \r\n carriage return encoded by notepad if that's how you created the text file manually.
    > if you debug, the debugger should show you all the whitespace characters so you can compare the contents of the file with the
    > string variable, and see what is the difference.
    >
    > let me know how you get on. by the way, i can't see where this relates to the first post, just trying to piece it together!
    > tim
    >
    >
    > "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message news:...
    >> thanks for the help. It looked like a promising solution but unfortunately did not solve my problem.
    >>
    >> I'm trying to read a string from a text file so I can encrypt it.
    >>
    >> When I create the text file manually, it reads and encrypts correctly.
    >>
    >> If I try to create the text file dynamically, it encrypts to a slightly different outcome.
    >>
    >> I can't understand why the difference.
    >>
    >> <code>
    >>
    >> Private Shared ReadOnly encryptionKeyBytes() As Byte = New Byte() {53, 53, 53, 53, 53, 53, 53, 53}
    >> Private Shared Function GenerateEncryptedLicFile(ByVal inputText As String, ByVal outputFile As String) As Boolean
    >>
    >> Dim outputStream As Stream = Nothing
    >> Dim encryptedStream As Stream = Nothing
    >> Dim result As Boolean = False
    >>
    >> Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider()
    >> des.Key = encryptionKeyBytes
    >> des.IV = encryptionKeyBytes
    >>
    >> Try
    >> outputStream = New FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)
    >>
    >> Dim desEncryptor As ICryptoTransform = des.CreateEncryptor()
    >> encryptedStream = New CryptoStream(outputStream, desEncryptor, CryptoStreamMode.Write)
    >>
    >> Dim contents() As Byte = des.EncryptValue(inputText)
    >>
    >> encryptedStream.Write(contents, 0, contents.Length)
    >> result = True
    >>
    >> Finally
    >> If Not encryptedStream Is Nothing Then
    >> encryptedStream.Close()
    >> encryptedStream = Nothing
    >> End If
    >> If Not outputStream Is Nothing Then
    >> outputStream.Close()
    >> outputStream = Nothing
    >> End If
    >> End Try
    >>
    >> Return result
    >> End Function
    >>
    >> </code>
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >> "Tim_Mac" <> wrote in message news:...
    >>> you could just use Trim() on the string which should remove any whitespace off either end of the line.
    >>> but if you want to use regex...
    >>>
    >>> \r is carriage return
    >>> \n is newline
    >>> a simple replace on these characters with the empty string will sort it out.
    >>> System.Text.RegularExpressions.Regex.Replace(input, @"\r\n", "");
    >>> hope this helps
    >>> tim
    >>>
    >>>
    >>>
    >>> "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message news:%...
    >>>> when reading a string from a file how to clean off "new line" and "end of line" stuff so text is returned as a clean single
    >>>> line ?
    >>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    Jon Paal, Sep 7, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas

    Custom Regular Expressions in ASP.net

    Jay Douglas, Nov 2, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    593
    mikeb
    Nov 3, 2003
  2. mark

    Regular expressions

    mark, Jun 30, 2003, in forum: Perl
    Replies:
    4
    Views:
    1,708
  3. Dustin D.
    Replies:
    1
    Views:
    11,128
  4. Jay Douglas
    Replies:
    0
    Views:
    582
    Jay Douglas
    Aug 15, 2003
  5. Noman Shapiro
    Replies:
    0
    Views:
    216
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page