Question Struggles with Regular Expressions

Joined
Jun 2, 2011
Messages
16
Programming Experience
3-5
I am writing an object to deal with state names. Sometimes I get the 2 letter abbreviation, and sometimes I get the full name. So I am trying to write an object that lets me deal with either. But I have a RegEx problem that I have been on for too long, and I'm stumped.

VB.NET:
Imports System.Text

Public Class State
 
    'States.txt is a text file of all of the American states in this format: "WA|Washington\nWV|West Virgina\n..." There is no line break at the end of the file

    Private Shared StatesString As String = IO.File.ReadAllText("States.txt")

    Public Sub New(ByVal NewState As String)

        Dim Match As RegularExpressions.Match
        Dim RegexOptions As RegularExpressions.RegexOptions = RegularExpressions.RegexOptions.Multiline _
                                                           Or RegularExpressions.RegexOptions.IgnoreCase

        If Len(NewState) = 2 Then
            'This branch works fine. The RegEx matches perfectly

            Match = RegularExpressions.Regex.Match(StatesString, "^" & NewState & "\|(?<FullName>[\w\s]+?)$", RegexOptions)

            If Match.Success = True Then
                mTwoLetter = UCase(NewState)
                mFullName = Match.Groups("FullName").Value
            Else
                Throw New ArgumentException(NewState & " is not a State")
            End If

        Else
            
            'This branch will not Match. You can replace the variable StatesString with a String literal like "WA|Washington" and it will match. 
            'And the object will behave as expected with the two Properties being set properly. 

            Match = RegularExpressions.Regex.Match(StatesString, "^(?<TwoLetter>[A-Z]{2})\|" & NewState & "$", RegexOptions)

            If Match.Success = True Then
                mTwoLetter = Match.Groups("TwoLetter").Value
                mFullName = StrConv(NewState, VbStrConv.ProperCase)
            Else
                 'This error gets thrown every time NewState is a valid state name such as "arizona"
                Throw New ArgumentException(NewState & " is not a State")
            End If
        End If

    End Sub

    Private mFullName As String
    Public ReadOnly Property FullName As String
        Get
            Return mFullName
        End Get
    End Property

    Private mTwoLetter As String
    Public ReadOnly Property TwoLetter As String
        Get
            Return mTwoLetter
        End Get
    End Property

End Class

If anyone can see what I am doing wrong, please help.
 
On the basis that Regex should be avoided at all costs in my book this seems to me like going all round the houses to achieve a very simple task. Firstly split the string on "\n" to give you an array of values such as "AZ|Arizona" .... "WV|West Virginia" and then the world's your oyster. You can further split and make a dictionary from this, or make a list, or a data table or ... well, you get the picture. Each of them has comparison functions available which will make this a breeze and you don't need regex for any of them.
 
Back
Top