Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27518

VS 2010 [RESOLVED] RegEx returning false match

$
0
0
I'm having an issue with my RegEx's match returning a false Success. In this case, I tested a blank space against this RegEx pattern:
Code:

(\.|-)*
And whenever I call:
Code:

If match.Success Then
The conditional statement returns a True value. I've checked my pattern here and the pattern only matches dots and hyphens, not the whitespace afterwards so I'm not sure why it's not preforming the same in my code. Here is the full code incase y'all want a peek:

Code:

Option Strict On
Option Explicit On
Module Lexer

    Private classes As List(Of List(Of String))

    Private Function LoadClasses() As List(Of List(Of String))
        Dim ticks As List(Of String) = New List(Of String)
        ticks.AddRange({"tick", "(\.|-)*"}) 'Match any "." or "-", 0 or more times

        Dim whitespace As List(Of String) = New List(Of String)
        whitespace.AddRange({"whitespace", "\s"}) 'Match any space, tab, or new line

        Dim comments As List(Of String) = New List(Of String)
        comments.AddRange({"comment", "#.*"}) 'Match any "#" and all leading characters until the new line

        Return {ticks, whitespace, comments}.ToList
    End Function

    Public Function Scan(ByVal source As String, ByRef tokens() As String, ByRef values() As String) As String()
        'Setup our token classes(if applicable)
        If IsNothing(classes) Then
            classes = LoadClasses()
        End If

        'Our ByRefs we'll be returning
        Dim tokenCollection As List(Of String) = New List(Of String)
        Dim valueCollection As List(Of String) = New List(Of String)

        'The exceptions we'll be returning(hopefully none)
        Dim exceptions As List(Of String) = New List(Of String)

        'Loop through each line
        For x As Integer = 0 To source.Split({Environment.NewLine}, StringSplitOptions.None).Count - 1
            Dim line As String = source.Split({Environment.NewLine}, StringSplitOptions.None)(x)

            'Loop through each word in that line
            For y As Integer = 0 To line.Split().Count - 1
                Dim word As String = line.Split()(y)
                Dim token As String = String.Empty

                'Loop through each token class
                For t As Integer = 0 To classes.Count - 1
                    Dim tokenClass As List(Of String) = classes.Item(t)

                    'Loop through each RegEx pattern in our class
                    For r As Integer = 1 To tokenClass.Count - 1
                        'Use RegEx to check for a match
                        Dim regex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(tokenClass.Item(r))
                        Dim match As System.Text.RegularExpressions.Match = regex.Match(word)

                        If match.Success Then
                            'If there is a match the set the token variable and leave the RegEx loop
                            token = tokenClass.Item(0)
                            Exit For
                        End If

                    Next 'End of RegEx

                    'Leave the token class loop if there is already a match
                    If Not String.IsNullOrWhiteSpace(token) Then
                        Exit For
                    End If

                Next 'End of token class

                If String.IsNullOrWhiteSpace(token) Then
                    exceptions.Add(String.Format("{0} is an unrecognizable character", word))
                Else
                    'Otherwise add it to the collection if it's not a comment
                    If token <> "comment" Then
                        tokenCollection.Add(token)
                        valueCollection.Add(word)
                    Else
                        'Add the newline token if there is one
                        If line.Substring(line.Length - 1) = Environment.NewLine Then
                            tokenCollection.Add("whitespace")
                            valueCollection.Add(Environment.NewLine)
                        End If
                    End If
                End If

            Next 'End of the word
        Next 'End of line

        'Don't return any tokens/values if there are exceptions
        If exceptions.Count > 0 Then
            tokenCollection = Nothing
            valueCollection = Nothing
        Else
            tokens = tokenCollection.ToArray
            values = valueCollection.ToArray
        End If

        Return exceptions.ToArray
    End Function

End Module


Viewing all articles
Browse latest Browse all 27518

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>