jessethebuilder
Member
- 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.
If anyone can see what I am doing wrong, please help.
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.