locked
Need help on Regex.replace RRS feed

  • Question

  • I have two textbox s . The first is my input text and the second is the result of replacements.

    Both are multi line.

    Textbox1 content looks something  like :


    5/23/2019 8:19:33 AM

    hello


    5/23/2019 8:19:33 AM

    How are you.

    .

    .

    .


    As you see , some lines are System.DateTime and I do not want to include them in replacements.

    I need to check if lines are not like the format of System.DateTime , then each character of the line should be changed to something else as string. For example "H" in hello replace with "rt" , "e" with "vc" and so on and so forth.

    after replacing each line of textbox1 (if not System.DateTime) , then the result of replacements should be put in textbox2.

    i mean textbox1 should change itself , but the result of replacements should be in textbox2


    I tired looping through textbox1.lines and using replace , method like this :

    Dim result As String
    For x as integer=0 to textbox1.lines.count -  1
    
             
    
    
                result = TextBox1.Lines(x).Replace("a", "vc")
    
    	    result = TextBox1.Lines(x).Replace("b", "cv")
    .
    .
    .
    
    next

    didn't work , and also I don't know how to exclude System.DateTime format.

    I want to use Regex

    P.S beside English alphabets , I also want to replace   "]"   "["   ";"   "'"  ","  "\" .



    Thursday, May 23, 2019 5:08 AM

Answers

  • Hi,

    can not be assigned like this:

      TextBox2.Lines(i) = lines(i).Replace("a", "vc")


    effect like this:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBox1.Text = "5/23/2019 8:19:33 AM
    
    aAbB
    
    "
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim lines As String() = TextBox1.Text.Split(vbLf)
            For Each line As String In lines
    
                If IsDate(line) = False And line <> vbCr Then
                    'TextBox1.Text += lines(i)
                    Dim t1 = line.Replace("a", "vc")
                    Dim t2 = t1.Replace("A", "bg")
                    Dim t3 = t2.Replace("b", "rg")
                    Dim t4 = t3.Replace("B", "yt")
                    TextBox2.Text += t4 & vbCrLf
                Else
                    TextBox2.Text += "" & vbCrLf
                End If
    
    
    
    
    
            Next
    
        End Sub
        Public Function IsDate(ByVal strDate As String) As Boolean
            Try
                DateTime.Parse(strDate)
                Return True
            Catch
                Return False
            End Try
        End Function
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Alex-KSGZ Thursday, May 23, 2019 9:25 AM
    • Marked as answer by Kevin993 Thursday, May 23, 2019 10:33 AM
    Thursday, May 23, 2019 9:22 AM

All replies

  • Hi,

    1.looping through textbox1.lines

     Dim lines As String() = TextBox1.Text.Split(vbLf)
            For i = 0 To lines.Count - 1
            ...
            Next

    2.check if lines are not like the format of System.DateTime

      Public Function IsDate(ByVal strDate As String) As Boolean
            Try
                DateTime.Parse(strDate)
                Return True
            Catch
                Return False
            End Try
        End Function

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 23, 2019 6:21 AM
  • Hi,

    1.looping through textbox1.lines

     Dim lines As String() = TextBox1.Text.Split(vbLf)
            For i = 0 To lines.Count - 1
            ...
            Next

    2.check if lines are not like the format of System.DateTime

      Public Function IsDate(ByVal strDate As String) As Boolean
            Try
                DateTime.Parse(strDate)
                Return True
            Catch
                Return False
            End Try
        End Function

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    I wrote this :

      

    >>> add the same number of lines in textbox1 to textbox2

    For x As Integer = 0 To TextBox1.Lines.Count - 1 TextBox2.Text &= vbNewLine Next Dim lines As String() = TextBox1.Text.Split(vbLf) For i = 0 To lines.Count - 1 If IsDate(lines(i)) = False Then TextBox2.Lines(i) = lines(i).Replace("a", "vc") TextBox2.Lines(i) = lines(i).Replace("A", "bg") TextBox2.Lines(i) = lines(i).Replace("b", "rg") TextBox2.Lines(i) = lines(i).Replace("B", "yt") end if

    Next

    What's the problem with the code ?!




    • Edited by Kevin993 Thursday, May 23, 2019 8:51 AM
    Thursday, May 23, 2019 8:50 AM
  • Hi,

    can not be assigned like this:

      TextBox2.Lines(i) = lines(i).Replace("a", "vc")


    effect like this:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBox1.Text = "5/23/2019 8:19:33 AM
    
    aAbB
    
    "
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim lines As String() = TextBox1.Text.Split(vbLf)
            For Each line As String In lines
    
                If IsDate(line) = False And line <> vbCr Then
                    'TextBox1.Text += lines(i)
                    Dim t1 = line.Replace("a", "vc")
                    Dim t2 = t1.Replace("A", "bg")
                    Dim t3 = t2.Replace("b", "rg")
                    Dim t4 = t3.Replace("B", "yt")
                    TextBox2.Text += t4 & vbCrLf
                Else
                    TextBox2.Text += "" & vbCrLf
                End If
    
    
    
    
    
            Next
    
        End Sub
        Public Function IsDate(ByVal strDate As String) As Boolean
            Try
                DateTime.Parse(strDate)
                Return True
            Catch
                Return False
            End Try
        End Function
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Alex-KSGZ Thursday, May 23, 2019 9:25 AM
    • Marked as answer by Kevin993 Thursday, May 23, 2019 10:33 AM
    Thursday, May 23, 2019 9:22 AM