Using Wildcards in List(Of String)
Hello, My name is TheMeq and this is my first post. So Hi.
I'll jump straight in.
I am making a code editor to go along side a program I am developing, it uses XML.
What I am looking to do is add syntax highlighting, which I have working for individual strings, but I may have some strings that have some changeable data.
Code:
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
Dim wordsRED As New List(Of String)
wordsRED.ADD("<key>")
wordsRED.ADD("</key>")
If RichTextBox1.Text.Length > 0 Then
Dim selectStart As Integer = RichTextBox1.SelectionStart
RichTextBox1.Select(0, RichTextBox1.Text.Length)
RichTextBox1.SelectionColor = Color.Black
RichTextBox1.DeselectAll()
'Red Colored Words
For Each oneWord As String In wordsRED
Dim pos As Integer = 0
Do While RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos) >= 0
pos = RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos)
RichTextBox1.Select(pos, oneWord.Length)
RichTextBox1.SelectionColor = Color.Red
pos += 1
Loop
Next
RichTextBox1.SelectionStart = selectStart
RichTextBox1.SelectionLength = 0
End If
End Sub
This works fine and dandy, but If I try to do something like
Code:
wordsRED.Add("<?xml*?>")
wordsRED.Add("<!DOCTYPE*>")
It doesn't recognise the * wildcard. So I am guessing that you can't do it this way, is there any other way to do it?
Thanks
Re: Using Wildcards in List(Of String)
First off - hi back and welcome.
What you will need to add to the list (and consequently search for) are regular expressions.
In your case something like "\<\?xml.+\?\>" and \<\!DOCTYPE.+\>".
Here's a link to the class you'll be using, and a link to an excellent tutorial on what regular expression can do in vb.net .
Regards Tom
(Oh and the Regexes I wrote here haven't been tested, so maybe you should write your own instead :))
Re: Using Wildcards in List(Of String)
Im guessing that class isn't a simple copy and paste in. It throws an error after i Imports System.Runtime.Serialization
Code:
Imports System.Runtime.Serialization
<SerializableAttribute()> _
Public Class Regex
Implements ISerializable
Dim instance As Regex
End Class
Error:
Code:
Class 'Regex' must implement 'Sub GetObjectData(info As SerializationInfo, contect As StreamingContext)' for interface 'System.Runtime.Serialization.ISerializable'.
Re: Using Wildcards in List(Of String)
You don't need to declare anything - the Regex class is already a part of the .Net framework.
Instead of using a List(Of String) you can use a List(Of Regex) and place elements into it by using wordsRED.Add(new Regex("your regular expression here")).
Instead of using the .IndexOf that you use with strings, you will be using the .IsMatch or .Match methods to match words/commands in your text.
If you have a CFG (or just a description) for the part of XML, you are implementing as your code-base, I could make a small parse example for you.
Tom
Re: Using Wildcards in List(Of String)
So how would I change this:
Code:
For Each oneWord As String In wordsPURPLE
Dim pos As Integer = 0
Do While RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos) >= 0
pos = RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos)
RichTextBox1.Select(pos, oneWord.Length)
RichTextBox1.SelectionColor = Color.Purple
pos += 1
Loop
Next
to work with that. If I change "For Each oneword As Regex In wordsPurple" it then underlines all the "oneWord.ToUpper" and "oneWord.Length"
Here is an example XML file that it would be using:
http://code.google.com/p/dreamboard/wiki/example
Re: Using Wildcards in List(Of String)
As a small example on using Regex to color syntactic xml-elements within a RichTextBox:
VB.NET Code:
Private Sub parse_xml_element_from_cursor_pos()
Static xml_element_parser As Regex = _
New Regex("\<(?<element>\w+)(?:\s(?<attribute>\w+)\=(?<value>[^\>\s/]+))*(?:\>|\s?/\>)", _
RegexOptions.Compiled)
Dim m As Match = xml_element_parser.Match(RichTextBox1.Text, RichTextBox1.SelectionStart)
If m.Success Then
'Color element green and ending > green
Dim element As Capture = m.Groups(xml_element_parser.GroupNumberFromName("element")).Captures(0)
RichTextBox1.Select(element.Index - 1, element.Length + 1) 'Include the beginning <
RichTextBox1.SelectionColor = Color.Green
RichTextBox1.Select(m.Index + m.Length - 1, 1) 'Ending >
RichTextBox1.SelectionColor = Color.Green
'Color attributes blue and the equality sign following red
For Each attribute As Capture In m.Groups(xml_element_parser.GroupNumberFromName("attribute")).Captures
RichTextBox1.Select(attribute.Index, attribute.Length)
RichTextBox1.SelectionColor = Color.Blue
RichTextBox1.Select(attribute.Index + attribute.Length, 1)
RichTextBox1.SelectionColor = Color.Red
Next
'Color values black
For Each value As Capture In m.Groups(xml_element_parser.GroupNumberFromName("value")).Captures
RichTextBox1.Select(value.Index, value.Length)
RichTextBox1.SelectionColor = Color.Black
Next
End If
End Sub