Program Purpose
So, I have a naming convention for certain folders.
I want to take in a folder name, and determine if it conforms to the convention.
Naming Convention
The convention (case insensitive) can be as simple as
"Surname, Firstname"
It could be as complicated as
"Surname (meta), Firstname (meta) & Firstname (meta) ; Surname (meta), Firstname (meta) & Firstname (meta)"
It is broken down like so:
A name is made up of a
[Surname]
and 1 or 2[Firstnames]
.Each
[Surname]
and[Firstname]
can have an optional[ (metadata)]
after it.If there are 2
[Firstnames]
, they are separated by[ & ]
.A name can, optionally, have a second set of
[Surname]
&[Firstnames]
. Separated from the first set by[ ; ]
.
As part of a larger program, I have a class object which handles information relating to a folder.
When a folder name is passed to the class, it validates the naming convention. It currently does this via regex but I find regex to be an incredible source of bugs and un-maintainable code.
So, is there a better way?
Program Flow
Receive folder name
Copy folder name
Regex Match/Replace the copy with
vbNullString
If the copy is now
vbNullString
, the whole string matched and is valid
Validation Code
Private Sub AddNamesFromClientFolder(ByVal ClientFolderName As String)
'/ Copy folder name
'/ Replace copy's regex matching with null string
'/ If the copy is now a null string, the whole name matched and is valid
'/ Client Folder names should be of the form:
'/ "[Surname] ( [misc] ), [Firstname] ( [Misc] ) & [Firstname] ( [Misc] ) ; [Other Surname] ( [Misc] ), [Other Firstname] ( [Misc] ) & [Other Firstname] ( [Misc] )"
'/
'/ With minimum form:
'/ "[Surname], [Firstname]"
Dim IsValid As Boolean
If Len(ClientFolderName) > 0 Then
Dim validationRegex As RegExp
Set validationRegex = New RegExp
With validationRegex
.Global = True
.IgnoreCase = False
.MultiLine = True
.Pattern = ClientFolderValidationRegex
End With
Dim testString As String
testString = ClientFolderName
testString = validationRegex.Replace(testString, vbNullString)
IsValid = (testString = vbNullString)
this.IsValid = IsValid
Else
this.IsValid = False
End If
End Sub
Building the regex pattern
Public Function ClientFolderValidationRegex() As String
'/ CG = "Capture Group"
Const L_CASE_LETTERS As String = "a-z"
Const U_CASE_LETTERS As String = "A-Z"
Const ALL_NUMBERS As String = "0-9"
Const NAME_PUNCTUATION As String = "`'!@\-_"
Const ALL_ALLOWED_PUNCTUATION As String = "`!""£$%^&*\-_+=\[\]{}:;@'~#<,>.?\/\\ "
Dim delim As String
'/ captures a single, contiguous group of letters/numbers/limited name punctuation e.g. "O'Malley"
Dim nameCG As String
nameCG = "([" & L_CASE_LETTERS & U_CASE_LETTERS & ALL_NUMBERS & NAME_PUNCTUATION & "]+)"
'/ captures the following: " (anything you want in here)"
Dim bracketedCG As String
bracketedCG = "( \(" & "([" & L_CASE_LETTERS & U_CASE_LETTERS & ALL_NUMBERS & ALL_ALLOWED_PUNCTUATION & "]+)" & "\))"
'/ Captures the following: "name (anything you want)" where " (anything you want)" may or may not be present
Dim nameSectionCG As String
nameSectionCG = "(" & nameCG & bracketedCG & "?" & ")"
'/ Surname portion of a filename should be the same as standard name section
Dim surnameCG As String
surnameCG = nameSectionCG
'/ Firstname portion might have an optional " & [name section]"
delim = " & "
Dim firstnameCG As String
firstnameCG = "(" & nameSectionCG & "(" & delim & nameSectionCG & ")?" & ")"
'/ Full name section of a filename is "[surname section], [firstname section]"
delim = ", "
Dim fullNamesCG As String
fullNamesCG = "(" & surnameCG & delim & firstnameCG & ")"
'/ Full filename might optionally have another " ; [full name section]"
delim = " ; "
Dim fullFilenameCG As String
fullFilenameCG = "(" & fullNamesCG & "(" & delim & fullNamesCG & ")?" & ")"
ClientFolderValidationRegex = fullFilenameCG
End Function
Regex Matching Examples:
Match:
Lannister, Tyrion
Lannister, Cersei (& Joffrey, Myrcella, Tommen {All Deceased})
Stark, Eddard (Ned, Deceased) ; Tully, Catelyn (Also Deceased)
No Match:
Tyrion Lannister
Lannister, Queen Cersei
Stark, Ned RED WEDDING
van Houten, Milhouse
andGödel, Kurt
should not match? \$\endgroup\$ – Hagen von Eitzen Jul 29 '16 at 21:59