patternMinor
Regex Matching a Naming Convention
Viewed 0 times
regexconventionmatchingnaming
Problem
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
-
Each
after it.
-
If there are 2
-
A name can, optionally, have a second set of
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
-
If the copy is now
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 =
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 validValidation 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 =
Solution
Have you ever heard about tussenvoegsels? They're parts of people's names. Well, in the Netherlands anyway. When used for authors, it's usually done as "van Surname, FirstName". Your regex doesn't support this, instead only accepting the last word of the surname. You should allow surnames to consist of multiple words.
What's the purpose of
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 IfWhat's the purpose of
IsValid here, if you're just going to nearly-directly write to this.IsValid anyway? Why not do it like this?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)
this.IsValid = (testString = vbNullString)
Else
this.IsValid = False
End IfCode Snippets
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 IfIf 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)
this.IsValid = (testString = vbNullString)
Else
this.IsValid = False
End IfContext
StackExchange Code Review Q#136325, answer score: 7
Revisions (0)
No revisions yet.