Hilfe beim ändern des Ausgabetextes (Regex)

  • VB.NET

Es gibt 14 Antworten in diesem Thema. Der letzte Beitrag () ist von Rinecamo.

    Hilfe beim ändern des Ausgabetextes (Regex)

    Hey VB-Paradise Com,
    ich habe derzeit Probleme mit einem "einfachen" Regex - Programm.

    Zum Programm: Ich verwende eine Textbox, Listbox und einen Button. Der Eingangstext wird in die Textbox eingetragen -> Ausgabetext ist in der Listbox.
    Beispiel: Der Inputtext ist: <test id="10" a="0" b="0" c="1" d="0" e="0" f="2"/> Der Ausgabetext soll sein (Listbox): TEST PROG `t_protocol` VALUES ('10', '0', '0', '1', '0', '0', '2');

    Mein Derzeitiger Programmcode sieht so aus:

    Quellcode

    1. Imports System.Text.RegularExpressions
    2. Public Class Form1
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4. Dim Gefunden As New List(Of String)
    5. Dim Curr As String
    6. Dim MC As MatchCollection = Regex.Matches(TextBox1.Text, "<test id="(.*)" a="(.*)" b="(.*)" c="(.*)" d="(.*)" e="(.*)" f="(.*)"/>", RegexOptions.IgnoreCase)
    7. For i As Integer = 0 To MC.Count - 1
    8. If Gefunden.Contains(MC(i).Value) = False Then
    9. Curr = MC(i).Value.ToString
    10. ListBox1.Items.Add(Curr)
    11. End If
    12. Next
    13. End Sub
    14. End Class

    Ich hoffe ihr könnt mir weiterhelfen.

    PS: Habe noch keine guten Regex kenntnisse.
    Mfg Regular_vb_user

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Regular_vb_user“ ()

    Kangaroo schrieb:

    Regular_vb_user schrieb:

    PS: Habe noch keine guten Regex kenntnisse.
    Das ist untertrieben: Du hast noch garkeine Regex-Kenntnisse :rolleyes:

    Lies Dir erst einmal dieses Tutorial durch und versuch es anzuwenden: RegEx Tutorial - Blutige Anfänger und Fortgeschrittene

    Dann helfen wir Dir gerne weiter ...


    Ich habe es bereits mit replace versucht, doch ich bekomme es nicht hin die variablen aus textbox1 auszulesen...

    Quellcode

    1. Imports System.Text.RegularExpressions
    2. Public Class Form1
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4. TextBox2.Text = Regex.Replace(TextBox1.Text, "<test id="(.*)" a="(.*)" b="(.*)" c="(.*)" d="(.*)" e="(.*)" f="(.*)"/>", "TEST PROG `t_protocol` VALUES ('(.*)', '(.*)', '(.*)', '(.*)', '(.*)', '(.*)', '(.*)');", RegexOptions.IgnoreCase)
    5. End Sub
    6. End Class


    Gibt es eine Möglichkeit über Replace ? oder nur über Split ?

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Regular_vb_user“ ()

    Quellcode

    1. Imports System.Text.RegularExpressions
    2. Public Class Form1
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4. Dim input As String = TextBox1.Text
    5. Dim filter1 As String = Regex.Replace(input, "a=" & "b=" & "c=" & "d=" & "e=" & "f=", ",")
    6. Dim erg As String = filter1
    7. TextBox2.Text = erg
    8. End Sub
    9. End Class


    So sieht mein derzeitiger code aus, doch der output text wird nicht abgeändert :S

    Weiß jemand woran das liegt, bzw. wie ich das ändern kann ?

    Mfg Regular_vb_user

    Regular_vb_user schrieb:

    hat sich erledigt. Hilft ja sowieso niemand...

    Da Du Dich bemüht hast:

    VB.NET-Quellcode

    1. Imports System.Text.RegularExpressions
    2. Public Class Form1
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4. Dim inputText = "<test id=""10"" a=""0"" b=""0"" c=""1"" d=""0"" e=""0"" f=""2""/>"
    5. ' entfernen wir erstmal die Anführungsstriche
    6. Dim cleanInput As String = inputText.Replace(""""c, "")
    7. ' regex pattern
    8. Dim pattern As String = "id=(?<ID>\d+) .*? a=(?<A>\d+) .*? b=(?<B>\d+) .*? c=(?<C>\d+) .*? d=(?<D>\d+) .*? e=(?<E>\d+) .*? f=(?<F>\d+)"
    9. Dim m As Match = Regex.Match(cleanInput, pattern, RegexOptions.IgnorePatternWhitespace Or RegexOptions.IgnoreCase)
    10. ' output
    11. Dim text As String = String.Format("TEST PROG 't_protocol' VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}' )", _
    12. m.Groups("ID").Value, m.Groups("A").Value, m.Groups("B").Value, m.Groups("C").Value, m.Groups("D").Value, m.Groups("E").Value, m.Groups("F").Value)
    13. MessageBox.Show(text)
    14. End Sub
    15. End Class

    Kangaroo schrieb:

    Imports System.Text.RegularExpressions
    Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim inputText = "<test id=""10"" a=""0"" b=""0"" c=""1"" d=""0"" e=""0"" f=""2""/>"
    ' entfernen wir erstmal die Anführungsstriche
    Dim cleanInput As String = inputText.Replace(""""c, "")
    ' regex pattern
    Dim pattern As String = "id=(?<ID>\d+) .*? a=(?<A>\d+) .*? b=(?<B>\d+) .*? c=(?<C>\d+) .*? d=(?<D>\d+) .*? e=(?<E>\d+) .*? f=(?<F>\d+)"
    Dim m As Match = Regex.Match(cleanInput, pattern, RegexOptions.IgnorePatternWhitespace Or RegexOptions.IgnoreCase)
    ' output
    Dim text As String = String.Format("TEST PROG 't_protocol' VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}' )", _
    m.Groups("ID").Value, m.Groups("A").Value, m.Groups("B").Value, m.Groups("C").Value, m.Groups("D").Value, m.Groups("E").Value, m.Groups("F").Value)
    MessageBox.Show(text)
    End Sub
    End Class


    Danke das du mir doch noch geholfen hast. In der zwischenzeit kam ich selbst zum ergebnis.

    Quellcode

    1. Imports System.Text.RegularExpressions
    2. Public Class query_converter
    3. Private Sub query_converter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4. End Sub
    5. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6. Dim input As String = TextBox1.Text
    7. Dim filter1 As String = Regex.Replace(input, "'", "")
    8. Dim erg As String = filter1
    9. TextBox2.Text = erg
    10. End Sub
    11. Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    12. Dim filter2 As String = Regex.Replace(TextBox2.Text, """", "'").Replace("<test id=", "TEST PROG `t_protocol` VALUES (").Replace("/>", ");")
    13. Dim erg As String = filter2
    14. TextBox3.Text = filter2
    15. End Sub
    16. Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
    17. Dim filter3 As String = Regex.Replace(TextBox3.Text, "a=", ",").Replace("b=", ",").Replace("c=", ",").Replace("d=", ",").Replace("e=", ",").Replace("f=", ",")
    18. Dim erg As String = filter3
    19. TextBox4.Text = filter3
    20. End Sub
    21. End Class


    Trotzdem danke, deine Version ist aufjedenfall besser, weil man dort die values in versch. reihenfolgen ausgeben lassen kann z.B. a, b ,d, f,c,e .
    Ich habe mein Projekt erweitert und stehe an einem weiterem Problem.

    Mein Code:

    Quellcode

    1. Imports System.Text.RegularExpressions
    2. Public Class Form1
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4. Dim input As String = TextBox3.Text
    5. Dim filter1 As String = Regex.Replace(input, "'", "")
    6. Dim erg As String = filter1
    7. TextBox2.Text = erg
    8. End Sub
    9. Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    10. Dim inputText = TextBox2.Text
    11. Dim cleanInput As String = inputText.Replace(""""c, "")
    12. Dim pattern As String = "id=(?<ID>\d+) .*? a=(?<A>\d+) .*? b=(?<B>\d+) .*? c=(?<C>\d+) .*? d=(?<D>\d+) .*? e=(?<E>\d+) .*? f=(?<F>\d+) .*? x=(?<X>\d+) .*? y=(?<Y>\d+)"
    13. Dim m As Match = Regex.Match(cleanInput, pattern, RegexOptions.IgnorePatternWhitespace Or RegexOptions.IgnoreCase)
    14. ' output
    15. Dim text As String = String.Format("TEST PROG 't_protocol' VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}' , '{7}', '{8}')", _
    16. m.Groups("ID").Value, m.Groups("A").Value, m.Groups("B").Value, m.Groups("C").Value, m.Groups("D").Value, m.Groups("E").Value, m.Groups("F").Value, m.Groups("X").Value, m.Groups("Y").Value)
    17. TextBox5.Text = text
    18. End Sub
    19. Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    20. Dim input As String = TextBox1.Text
    21. Dim filter1 As String = Regex.Replace(input, "/>", "x=100 y=200")
    22. TextBox3.Text = filter1
    23. End Sub
    24. End Class


    Der Inputtext in Textbox1 lautet wie folgt: <test id="10" a="0" b="0" c="1" d="0" e="0" f="2"/>
    Der Ausgabetext wird um X=100 und Y=200 erweitert -> Ausgabetext soll sein: TEST PROG `t_protocol` VALUES ('10', '0', '0', '1', '0', '0', '2', '100', '200');
    Das Funktioniert bereits mit meinem Code... Mein aktuelles Problem ist, dass sich dieser Code nur für die erste Zeile des Inputtextes auswirkt -> Rest wird ignoriert.

    #Die Values X=100 und Y=200 sind fest im Programm eingestellt und sollen es auch bleiben!
    Beispiel: Inputtext:

    Quellcode

    1. <test id="10" a="0" b="0" c="1" d="0" e="0" f="2"/>
    2. <test id="50" a="10" b="20" c="5" d="60" e="20" f="7"/>

    Ausgabetext:

    Quellcode

    1. TEST PROG `t_protocol` VALUES ('10', '0', '0', '1', '0', '0', '2', '100', '200');

    Ich habe es bereits mit

    Quellcode

    1. Dim pattern As String = "id=(?<ID>\d+) .*? a=(?<A>\d+) .*? b=(?<B>\d+) .*? c=(?<C>\d+) .*? d=(?<D>\d+) .*? e=(?<E>\d+) .*? f=(?<F>\d+) .*? x=(?<X>\d+) .*? y=(?<Y>\d+)"
    2. For Each listMatch As Match In Regex.Matches(inputtext, pattern, RegexOptions.Singleline Or RegexOptions.IgnoreCase)
    3. Dim m As Match = Regex.Match(cleanInput, pattern, RegexOptions.IgnorePatternWhitespace Or RegexOptions.IgnoreCase)
    4. Dim text As String = String.Format("TEST PROG 't_protocol' VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}' , '{7}', '{8}')", _
    5. m.Groups("ID").Value, m.Groups("A").Value, m.Groups("B").Value, m.Groups("C").Value, m.Groups("D").Value, m.Groups("E").Value, m.Groups("F").Value, m.Groups("X").Value, m.Groups("Y").Value)
    6. TextBox5.Text = text
    7. Next

    versucht, doch dies funktioniert nicht...

    Wie kann ich meinen Code so umbauen, dass ich damit mehrere Ereignisse des Inputtextes ausgeben kann ?

    Mfg Regular_vb_user

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „der_Kurt“ () aus folgendem Grund: Farbe ROT entfernt

    VB.NET-Quellcode

    1. Module Module1
    2. Sub main()
    3. Dim inputText As String = "<test id=""10"" a=""0"" b=""0"" c=""1"" d=""0"" e=""0"" f=""2""/>" & vbCrLf & _
    4. "<test id=""50"" a=""10"" b=""20"" c=""5"" d=""60"" e=""20"" f=""7""/>"
    5. Dim t() As String, tmp As String, ausgabetext As String
    6. t = Split(inputText, vbCrLf)
    7. For i As Integer = 0 To t.Length - 1
    8. tmp = t(i).Replace(Chr(34), "'").Replace("=", ", ")
    9. tmp = tmp.Replace("<test id, ", "(").Replace("/>", " '100', '200');")
    10. tmp = tmp.Replace(" a", "").Replace(" b", "").Replace(" c", "")
    11. tmp = tmp.Replace(" d", "").Replace(" e", "").Replace(" f", "")
    12. ausgabetext &= "TEST PROG `t_protocol` VALUES " & tmp & vbCrLf
    13. Next
    14. 'TextBox5.Text = ausgabetext
    15. Debug.Print(ausgabetext)
    16. End Sub
    17. End Module

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Eierlein“ ()

    Hallo,

    hast du dir das so vergestellt?

    VB.NET-Quellcode

    1. Dim inputText As String = "<test id=""10"" a=""0"" b=""0"" c=""1"" d=""0"" e=""0"" f=""2""/>" & vbCrLf & _
    2. "<test id=""50"" a=""10"" b=""20"" c=""5"" d=""60"" e=""20"" f=""7""/>"
    3. Dim t() As String, tmp As String
    4. t = Split(inputText, vbCrLf)
    5. Dim ausgabetext(t.Length - 1) As String
    6. For i As Integer = 0 To t.Length - 1
    7. tmp = t(i).Replace(Chr(34), "'").Replace("=", ", ")
    8. tmp = tmp.Replace("<test id, ", "(").Replace("/>", " '100', '200');")
    9. tmp = tmp.Replace(" a", "").Replace(" b", "").Replace(" c", "")
    10. tmp = tmp.Replace(" d", "").Replace(" e", "").Replace(" f", "")
    11. ausgabetext(i) = "TEST PROG `t_protocol` VALUES " & tmp
    12. Next
    13. For Each a In ausgabetext
    14. MessageBox.Show(a)
    15. Next


    mfG

    Derfuhr
    ich hab hier einen Pattern, der alles gequotete selektiert.
    Das wird dann jeweils in SingleQuotes gepackt, und dann wieder zu einen String zusammengesetzt, mit Komma dazwischen:

    VB.NET-Quellcode

    1. Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    2. Dim inputText = "<test id=""10"" a=""0"" b=""0"" c=""1"" d=""0"" e=""0"" f=""2""/>"
    3. Dim rgx = New Regex(".*?""(.*?)""")
    4. Dim mts = From mt In rgx.Matches(inputText).Cast(Of Match)() Select String.Concat("'", mt.Groups(1).Value, "'")
    5. Dim text = String.Join(", ", mts)
    6. MessageBox.Show(text)
    7. End Sub

    Ausgabe:

    Quellcode

    1. '10', '0', '0', '1', '0', '0', '2'
    Das Drumrum sollte der TE dann selber hinkriegen.

    zu beachten, dass der Regex auf zeilenweises einlesen ausgelegt ist.

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „ErfinderDesRades“ ()

    @ErfinderDesRades: Deine Lösung würde jedoch nur funktionieren, wenn außer den "<test .../>"'s nichts anderes im Quellcode steht.

    Sollte auch funktionieren, wenn auch noch was anderes als <test .../> drin steht.

    VB.NET-Quellcode

    1. For Each m As Match in Regex.Matches(content, "(?<=\<test\s)(\w{1,2}\=""\d+""[\s/])+"))
    2. Dim s As String = String.Join(", ", (From mc As Match in Regex.Matches(m.Value, "(?<=\="")\d+(?="")") Select String.Format("'{0}'", mc.Value)))
    3. Console.WriteLine(String.Format("{0}{1}{2}", "TEST PROG `t_protocol` VALUES (", s, ");")
    4. Next
    Ist in C# geschrieben und im Editor hier übersetzt (deshalb auch der Klammerfehler). Wieso VB bei dem Linq-Query meckert ist mir schleierhaft.

    Verbessert:

    VB.NET-Quellcode

    1. Dim Content As String = "<test id=""10"" a=""0"" b=""0"" c=""1"" d=""0"" e=""0"" f=""2""/>"
    2. For Each m As Match In Regex.Matches(Content, "(?<=\<test\s)(\w{1,2}\=""\d+""[\s/])+")
    3. Dim s As String = String.Join(", ", (From mc In Regex.Matches(m.Value, "(?<=\="")\d+(?="")").Cast(Of Match)() Select String.Format("'{0}'", mc.Value)))
    4. Console.WriteLine(String.Format("{0}{1}{2}", "TEST PROG `t_protocol` VALUES (", s, ");"))
    5. Next