Results 1 to 2 of 2

Thread: Regex SQL syntax highlighting

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2010
    Posts
    164

    Regex SQL syntax highlighting

    Hi all,
    I decided to view my sql queries in a richtext box but when I loaded my query obviously I cannot syntax highlighting like SSMS.
    So I decided to use regex to plit each line and when I find some words like select, I change colour to it.
    This is code
    Code:
    ...
      Dim s As Regex = New Regex("([ \,;()'*-])")
     Dim word1 As String()
    word1 = s.Split(linea)
    For i = 0 To word1.Length - 1
                        RichTextBox1.SelectionFont = New Font("Courier New", 10, FontStyle.Regular)
                        RichTextBox1.SelectionColor = Color.Black
                        keywords = {"select", "distinct", "case", "when", "end", "as", "else", "then", "varchar", "where", "on", "from"}
    If keywords.Contains(LCase(word1(i))) Then
    
                            RichTextBox1.SelectionColor = Color.Blue
                            RichTextBox1.SelectionFont = New Font("Courier New", 10, FontStyle.Bold)
      End If
    RichTextBox1.SelectedText = word1(i)
    ...
    I don't know if it a best method but it works fine.

    My issue in this method is to highlight sql comments like
    Code:
    /****** Script for SelectTopNRows command from SSMS  ******/
    or
    --,case when len(a.[mycity])=5 then c.mycity else a.mycity end as mycity
    For first string
    regex splis it as:
    Code:
    "/"
    "*"
    ""
    "*"
    ""
    "*"
    ...
    So I don't know why in my split I find "" and I don't know which is regex syntax to remove it.

    How can I remove "" char?

    Thank in advanced
    gio

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Regex SQL syntax highlighting

    At this moment I can think of no better answer than to suggest this tutorial: https://www.tutorialspoint.com/vb.ne...xpressions.htm.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width