Program PurposeSo, 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 ConventionThe convention (case insensitive) can be as simple asSurname, FirstnameIt could be as complicated asSurname (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 FlowReceive folder nameCopy folder nameRegex Match/Replace the copy with vbNullStringIf the copy is now vbNullString, the whole string matched and is validValidation CodePrivate 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 IfEnd SubBuilding the regex patternPublic 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 = fullFilenameCGEnd FunctionLink to regex101Regex Matching Examples:Match:Lannister, TyrionLannister, Cersei (& Joffrey, Myrcella, Tommen {All Deceased})Stark, Eddard (Ned, Deceased) ; Tully, Catelyn (Also Deceased)No Match:Tyrion LannisterLannister, Queen CerseiStark, Ned RED WEDDING
Regex Matching a Naming Convention
regex;vba;file system;file structure
Why are you using regex for this? It makes no sense to match complicated naming conventions with something as low-level as Regular Expressions. You either have to go higher in your abstraction by using a proper Grammar (which is basically what your separated groups do, but better) or go lower to forego the semantics you impose here.Consider the following pseudo-ish code:Dim isValid As BooleanDim Names(2) As StringisValid = True Names = ClientFolderName.Split( ; )For Each Name As String In Names    isValid = isValid And IsValidName(Name)NextThis drops the first barrier that's overcomplicating your regex: the fact that it may contain two things.Since you're only interested in validity you can use the following Function to check the validity of it's sub-partsFunction IsValidName(Name As String) As Boolean    Dim NameParts(2) As String    Dim result As Boolean    result = True    NameParts = Name.Split(, )    result = result And IsValidSurname(NameParts(1))    result = result And IsValidFirstname(NameParts(2))    IsValidName = resultEnd FunctionIsValidSurname and IsValidFirstname are significantly easier to implement and understand with regex than trying to instantly validate the whole thing with Regex. In addition to being much more maintainable you also get to have separate responsibilities as a bonus.Follow great advice from someone who's come ages before us:Divide and ConquerThis technique does not only apply in military, it's also a hugely useful and important skill during software development. 