Search code examples
regexstringc-strings

VB.NET Regular expression 0-9 and a-z not working?


How can i make a regex expression for numbers and letters? whenever i input it only reads the strings in order not randomize. lets say i input 312 <-- it would be invalid identifier because its number variable is not in order. On letters i tried a123456789 so whenver i input a1, a2, a3 it reads as identifier because its in order. and i wanted also to create another variable that will combine numbers and letters.

    Dim input As String = txtInput.Text
    Dim symbol As String = "\s*([-+*/=])\s*"
    Dim numbers As String = "123456789" //("[0-9]") <-- doesnt work?
    Dim letters As String = "abcdefghijklmnopqrstuvwxyz" // ("[a-z]")<-also

    Dim substrings() As String = Regex.Split(input, symbol)

    For Each match As String In substrings
        If symbol.Contains(match) Then
            lstOutput.Items.Add(match & " - operator")
        ElseIf numbers.Contains(match) Then
            lstOutput.Items.Add(match & " - number")
        ElseIf letters.Contains(match) Then
            lstOutput.Items.Add(match & " - identifier")
        Else
            lstOutput.Items.Add(match & " - Invalid Identifier")
        End If
    Next

Input: c1 + 2c + cad + c1b

expected output:

c1 - identifier

    • operator

2c - invalid identifier // invalid because the 1st character is number

    • operator

cad - identifier

    • operator

c1b - identifier


Solution

  • You may use regex patterns for numbers and identifiers with Regex.IsMatch.

    Number pattern:

    • ^ - start of string
    • [0-9]* - 0+ digits
    • \.? - an optional dot
    • [0-9]+ - 1+ digits
    • $ - end of string

    Identifier pattern:

    • ^ - start of string
    • [a-zA-Z_] - an ASCII letter or _
    • [a-zA-Z0-9_]* - 0+ ASCII letters, digits or _
    • $ - end of string.

      Dim input As String = "c1 + 2c + cad + c1b" Dim symbol As String = "\s*([-+/=])\s" Dim numbers As String = "^[0-9].?[0-9]+$" Dim letters As String = "^[a-zA-Z_][a-zA-Z0-9_]$"

      Dim substrings() As String = Regex.Split(input, symbol)

      For Each match As String In substrings If Regex.IsMatch(match, symbol) Then Console.WriteLine(match & " - operator") ElseIf Regex.IsMatch(match, numbers) Then Console.WriteLine(match & " - number") ElseIf Regex.IsMatch(match, letters) Then Console.WriteLine(match & " - identifier") Else Console.WriteLine(match & " - Invalid Identifier") End If Next

    See the VB.NET demo outputting

    c1 - identifier
    + - operator
    2c - Invalid Identifier
    + - operator
    cad - identifier
    + - operator
    c1b - identifier