Results 1 to 6 of 6

Thread: Using Wildcards in List(Of String)

  1. #1
    New Member
    Join Date
    Aug 12
    Location
    Nottingham, UK
    Posts
    3

    Talking 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

  2. #2
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    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 )
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  3. #3
    New Member
    Join Date
    Aug 12
    Location
    Nottingham, UK
    Posts
    3

    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'.

  4. #4
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    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
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  5. #5
    New Member
    Join Date
    Aug 12
    Location
    Nottingham, UK
    Posts
    3

    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

  6. #6
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    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:
    1. Private Sub parse_xml_element_from_cursor_pos()
    2.  
    3.         Static xml_element_parser As Regex = _
    4.             New Regex("\<(?<element>\w+)(?:\s(?<attribute>\w+)\=(?<value>[^\>\s/]+))*(?:\>|\s?/\>)", _
    5.                       RegexOptions.Compiled)
    6.         Dim m As Match = xml_element_parser.Match(RichTextBox1.Text, RichTextBox1.SelectionStart)
    7.  
    8.         If m.Success Then
    9.  
    10.             'Color element green and ending > green
    11.             Dim element As Capture = m.Groups(xml_element_parser.GroupNumberFromName("element")).Captures(0)
    12.  
    13.             RichTextBox1.Select(element.Index - 1, element.Length + 1) 'Include the beginning <
    14.             RichTextBox1.SelectionColor = Color.Green
    15.             RichTextBox1.Select(m.Index + m.Length - 1, 1) 'Ending >
    16.             RichTextBox1.SelectionColor = Color.Green
    17.  
    18.             'Color attributes blue and the equality sign following red
    19.             For Each attribute As Capture In m.Groups(xml_element_parser.GroupNumberFromName("attribute")).Captures
    20.  
    21.                 RichTextBox1.Select(attribute.Index, attribute.Length)
    22.                 RichTextBox1.SelectionColor = Color.Blue
    23.                 RichTextBox1.Select(attribute.Index + attribute.Length, 1)
    24.                 RichTextBox1.SelectionColor = Color.Red
    25.  
    26.             Next
    27.  
    28.             'Color values black
    29.             For Each value As Capture In m.Groups(xml_element_parser.GroupNumberFromName("value")).Captures
    30.  
    31.                 RichTextBox1.Select(value.Index, value.Length)
    32.                 RichTextBox1.SelectionColor = Color.Black
    33.  
    34.             Next
    35.  
    36.         End If
    37.  
    38.     End Sub
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

Posting Permissions

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