In a large method, I have to define if the version value in a magazine name - issue number is V1 thru vN (up to 5 as writing time, but this may vary).
For testing, I use a check on " v1-" Or " v2-" or " v3-" … Then
Isn’t it a short possible test ?
the full field can be:
“Magazine Name v1-001” (other fields removed; the separator is " - ").
“Magazine Name v2-001”
“Magazine Name v3-001”
“Magazine Name v4-001”
“Magazine Name v5-001”
and maybe others…
Ideas ?
The value that is between " v" and “-” ( v1-) is used, but not tested nor computed. It is a simple checking to avoid confusion with a magazine with two or three words where the later word start with a … v ! (like in “Magazine value” for example)
Schematically, the code is:
If VolName holds " v1-" Then // Or " v2-" Or " v3-" Or " v4-"…
// Do something
Else
// Do something else
End If
A full line is
` Magazine Name v1-001 - Price - Date - Pages - Editor - Comments
At last, I extract the magazine name, the version (or series, or volume, …) number, the issue number (since less than 999, I format it to three digits even if only one or two appears in the line).
At last all these information will appear one field in itw own line (6 lines actually).
I don‘t know the context of the project, but I would have stored each magazine in an object with name, price etc. as properties. Then you could do something like „If Magazine.IssueNumber >= 1 And Magazine.IssueNumber <= n“. For the complete line I would implement a getter method in the Magazine class that returns a concatenated String.
But as I said, I don‘t know what your application does, so maybe a different approach would be more useful.
dim rx as new RegEx
rx.SearchPattern = "(?mi-Us)v\d-001"
dim rxOptions as RegExOptions = rx.Options
rxOptions.LineEndType = 4
dim match as RegExMatch = rx.Search( sourceText )
while match isa RegExMatch
//
// Do something
//
match = rx.Search // Fetch the next match
wend
Split on the Dash. Now you have all the fields as separate elements in an array.
The first element is Magazine Name vdigit. If you do not want to bother with Regex, look for " v", get the location and peel off the right side (end) into a separate field.
The next element is 001 or 002. Save it as a number and then you can format it as a string any way you want.
In this way, the data is pretty easily broken up into its components and then you can rejoin them however you please.
I may face an error in the future if I found a magazine with a name like “Pourquoi-pas v2-012” (or “Why-Not v2-012 ?", or “Look-In v5-123” who exists in the U.K.)
is the natural way of thinking; but I totally forgot abour RegEx. I thinkl with time that my brain is selective (excluding the obvious and is less creative).
The real deal is in the number (d above) and RegEx really knows how to deal with it.
Of course, this is far more lines than the if block, but will not break because the volume number is outofbounds…
I will probably create a Function that returns a Boolean (isVolume As Boolean // not related to mass storage), so the main code will continue to be “clean” and do a simple If …/ Then block.