C
Chris
Hi everyone,
I'm trying to parse through the contents of some text files with regular
expressions, but am new to regular expressions and how to use them in
VB.net.
I'm pretty sure that the regular expressions are correct as I got them from
regexlib.com and tested them in the Regulator and Expresso.
The problem is I tested this function with a file that contains a string
which should be seen by the SSNRegex. This makes me think that something is
definitely wrong in my program.
Any help would be awesome.
Thanks in advance.
Chris
Code to follow:
Imports System.Text.RegularExpressions
''' <summary>
''' U.S. social security numbers (SSN), within the range of numbers
''' that have been currently allocated. Matches the pattern AAA-GG-SSSS,
''' AAA GG SSSS, AAA-GG SSSS, AAA GG-SSSS, AAAGGSSSS, AAA-GGSSSS,
AAAGG-SSSS,
''' AAAGG SSSS or AAA GGSSSS. All zero in any one field is not allowed.
'''** Additionally, spaces and/or dashes and/or nothing are allowed.
''' Thanks to Joe Johnston and Regexlib.com for this Regex
''' </summary>
''' <remarks></remarks>
Dim ssnRegex As String = "^(?!000)([0-6]\d{2}|7([0-6]\d|7[012])) ([ -])?
(?!00)\d\d([ -|])? (?!0000)\d{4}$"
''' <summary>
''' Matches major credit cards including: Visa (length 16, prefix 4);
''' Mastercard (length 16, prefix 51-55);
''' Diners Club/Carte Blanche (length 14, prefix 36, 38, or 300-305);
''' Discover (length 16, prefix 6011); American Express (length 15,
prefix 34 or 37).
''' Saves the card type as a named group to facilitate further
validation against a "card type";
''' checkbox in a program. All 16 digit formats are grouped 4-4-4-4
with an optional hyphen or
''' space between each group of 4 digits. The American Express format is
grouped 4-6-5 with an
''' optional hyphen or space between each group of digits. Formatting
characters must be consistant,
''' i.e. if two groups are separated by a hyphen, all groups must be
separated by a hyphen for a
''' match to occur. Thanks to Steven Smith and Regexlib.com for this
Regex
''' </summary>
''' <remarks></remarks>
Dim creditCardRegex As String =
"^(??<Visa>4\d{3})|(?<Mastercard>5[1-5]\d{2})|(?<Discover>6011)|(?<DinersClub>(?:3[68]\d{2})|(?:30[0-5]\d))|(?<AmericanExpress>3[47]\d{2}))([
-]?)(?(DinersClub)(?:\d{6}\1\d{4})|(?(AmericanExpress)(?:\d{6}\1\d{5})|(?:\d{4}\1\d{4}\1\d{4})))$" Dim options As System.Text.RegularExpressions.RegexOptions = _ (System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespaceOr _ System.Text.RegularExpressions.RegexOptions.Singleline Or _ System.Text.RegularExpressions.RegexOptions.Compiled Or _ System.Text.RegularExpressions.RegexOptions.IgnoreCase) Dim ssnRegex As New Regex(RegExTypes.ssnRegex, RegExTypes.options) Dim creditCardRegex As New Regex(RegExTypes.creditCardRegex,RegExTypes.options) Public Function CheckContents(ByVal Filename As String, ByRefFileContents As String) As Boolean '''''Check File Contents Against Regex''''' 'Not sure if this is correct. Dim ssnMatches As MatchCollection =ssnRegex.Matches(FileContents) Dim creditCardMatches As MatchCollection =creditCardRegex.Matches(FileContents) '''''Process any Matches''''' 'SSN matches For Each match As Match In ssnMatches Dim newFoundItem As New foundItem newFoundItem.fileName = Filename newFoundItem.TypeOfAsset = SSN foundItemsArray.Add(newFoundItem) Next 'Credit Card matches For Each match As Match In creditCardMatches 'Create a New Found Item Dim newFoundItem As New foundItem 'foundItem is just astruct in another file 'Set the File location newFoundItem.fileName = Filename'Here I'm trying to capture the named grouped from the regex. Not sure ifthis is correct. 'Determine the CreditCard type by Regex match group Select Case True Case "Visa" = match.Groups.Item("Visa").Value newFoundItem.TypeOfAsset = VISA Case "Mastercard" =match.Groups.Item("Mastercard").Value newFoundItem.TypeOfAsset = MASTERCARD Case "DinersClub" =match.Groups.Item("DinersClub").Value newFoundItem.TypeOfAsset = DINERS_CLUB Case "AmericanExpress" =match.Groups.Item("AmericanExpress").Value newFoundItem.TypeOfAsset = AMERICAN_EXPRESS Case Else newFoundItem.TypeOfAsset = CREDITCARD End Select 'Add the newFoundItem to our array foundItemsArray.Add(newFoundItem) Next If ssnMatches.Count > 0 Or creditCardMatches.Count > 0 Then Return True Else Return False End If End Function
I'm trying to parse through the contents of some text files with regular
expressions, but am new to regular expressions and how to use them in
VB.net.
I'm pretty sure that the regular expressions are correct as I got them from
regexlib.com and tested them in the Regulator and Expresso.
The problem is I tested this function with a file that contains a string
which should be seen by the SSNRegex. This makes me think that something is
definitely wrong in my program.
Any help would be awesome.
Thanks in advance.
Chris
Code to follow:
Imports System.Text.RegularExpressions
''' <summary>
''' U.S. social security numbers (SSN), within the range of numbers
''' that have been currently allocated. Matches the pattern AAA-GG-SSSS,
''' AAA GG SSSS, AAA-GG SSSS, AAA GG-SSSS, AAAGGSSSS, AAA-GGSSSS,
AAAGG-SSSS,
''' AAAGG SSSS or AAA GGSSSS. All zero in any one field is not allowed.
'''** Additionally, spaces and/or dashes and/or nothing are allowed.
''' Thanks to Joe Johnston and Regexlib.com for this Regex
''' </summary>
''' <remarks></remarks>
Dim ssnRegex As String = "^(?!000)([0-6]\d{2}|7([0-6]\d|7[012])) ([ -])?
(?!00)\d\d([ -|])? (?!0000)\d{4}$"
''' <summary>
''' Matches major credit cards including: Visa (length 16, prefix 4);
''' Mastercard (length 16, prefix 51-55);
''' Diners Club/Carte Blanche (length 14, prefix 36, 38, or 300-305);
''' Discover (length 16, prefix 6011); American Express (length 15,
prefix 34 or 37).
''' Saves the card type as a named group to facilitate further
validation against a "card type";
''' checkbox in a program. All 16 digit formats are grouped 4-4-4-4
with an optional hyphen or
''' space between each group of 4 digits. The American Express format is
grouped 4-6-5 with an
''' optional hyphen or space between each group of digits. Formatting
characters must be consistant,
''' i.e. if two groups are separated by a hyphen, all groups must be
separated by a hyphen for a
''' match to occur. Thanks to Steven Smith and Regexlib.com for this
Regex
''' </summary>
''' <remarks></remarks>
Dim creditCardRegex As String =
"^(??<Visa>4\d{3})|(?<Mastercard>5[1-5]\d{2})|(?<Discover>6011)|(?<DinersClub>(?:3[68]\d{2})|(?:30[0-5]\d))|(?<AmericanExpress>3[47]\d{2}))([
-]?)(?(DinersClub)(?:\d{6}\1\d{4})|(?(AmericanExpress)(?:\d{6}\1\d{5})|(?:\d{4}\1\d{4}\1\d{4})))$" Dim options As System.Text.RegularExpressions.RegexOptions = _ (System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespaceOr _ System.Text.RegularExpressions.RegexOptions.Singleline Or _ System.Text.RegularExpressions.RegexOptions.Compiled Or _ System.Text.RegularExpressions.RegexOptions.IgnoreCase) Dim ssnRegex As New Regex(RegExTypes.ssnRegex, RegExTypes.options) Dim creditCardRegex As New Regex(RegExTypes.creditCardRegex,RegExTypes.options) Public Function CheckContents(ByVal Filename As String, ByRefFileContents As String) As Boolean '''''Check File Contents Against Regex''''' 'Not sure if this is correct. Dim ssnMatches As MatchCollection =ssnRegex.Matches(FileContents) Dim creditCardMatches As MatchCollection =creditCardRegex.Matches(FileContents) '''''Process any Matches''''' 'SSN matches For Each match As Match In ssnMatches Dim newFoundItem As New foundItem newFoundItem.fileName = Filename newFoundItem.TypeOfAsset = SSN foundItemsArray.Add(newFoundItem) Next 'Credit Card matches For Each match As Match In creditCardMatches 'Create a New Found Item Dim newFoundItem As New foundItem 'foundItem is just astruct in another file 'Set the File location newFoundItem.fileName = Filename'Here I'm trying to capture the named grouped from the regex. Not sure ifthis is correct. 'Determine the CreditCard type by Regex match group Select Case True Case "Visa" = match.Groups.Item("Visa").Value newFoundItem.TypeOfAsset = VISA Case "Mastercard" =match.Groups.Item("Mastercard").Value newFoundItem.TypeOfAsset = MASTERCARD Case "DinersClub" =match.Groups.Item("DinersClub").Value newFoundItem.TypeOfAsset = DINERS_CLUB Case "AmericanExpress" =match.Groups.Item("AmericanExpress").Value newFoundItem.TypeOfAsset = AMERICAN_EXPRESS Case Else newFoundItem.TypeOfAsset = CREDITCARD End Select 'Add the newFoundItem to our array foundItemsArray.Add(newFoundItem) Next If ssnMatches.Count > 0 Or creditCardMatches.Count > 0 Then Return True Else Return False End If End Function