Have to convert text(C code) written in richtextBox to Html with automatic syntax highlighting and indention.How can one achieve that using Regex in vb.net.
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
Dim Str As String = ""
Dim line As String = ""
Dim Keywords() As String = New String() {"auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "elseif", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while"}
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim brks() As String = New String() {"{", ";", "}", "if", "else", "elseif"}
Dim HtmlWriter As StreamWriter = New StreamWriter("C://Code.html")
Dim key As String = ""
Dim i As Integer = 0
Dim TabCnt As Integer = 0
Str = " "
line = " "
line = RichTextBox1.Text
Str &= "<html><body>"
For Each line In RichTextBox1.Lines()
indent()
ParseLine()
ParseKeys()
For Each brk As String In brks
If InStr(line, brk) Then
line = line.Replace(brk, "<br>" & brk)
If brk = ";" Then
line = line.Replace(brk, brk & "<br>")
End If
End If
Next
Str &= line & "<br>"
Next
Str &= "</body></html>"
HtmlWriter.Write(Str)
HtmlWriter.Close()
MessageBox.Show(Str)
End Sub
Private Sub ParseLine()
Dim token As String = "([{\\t;:()}])"
Dim Reg As Regex = New Regex(token)
Dim i As Integer = 0
Dim tokens() As String = Reg.Split(line)
For i = 0 To (tokens.Length - 1)
MessageBox.Show(tokens(i).ToString())
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Str = " "
line = " "
End Sub
Private Sub ParseKeys()
Dim Txt As String = ""
Dim key As String = ""
Dim r As Regex = New Regex("\w+")
Dim line1 As String = line
Dim matches As Match
Dim index As Integer = 0
Dim i As Integer = 0
While (True)
matches = r.Match(line1.Substring(index))
If (matches.Success) Then
Txt = matches.ToString()
i = 0
For Each key In Keywords
If Txt = key Then
Txt = "<font color=blue>" & Txt & "</font>"
line1 = line1.Remove(matches.Index + index, matches.Length)
line1 = line1.Insert(matches.Index + index, Txt)
index = index + 25 + matches.Length
i = 1
Exit For
End If
Next
If i = 0 Then
index = index + Txt.Length + 1
End If
If index > line1.Length Then
Exit While
End If
matches = matches.NextMatch()
End If
End While
line = line1
End Sub
Private Sub indent()
Dim r As Regex = New Regex("^{\s*(\w*)\s*}$") 'incomplete code
End Sub