There is a logic issue in previous code.
Working on a function to help with parsing. Will post back later.
Here is revised function code and Test program and results from running each of the input test strings. This deals with any characters beyond PatternB in the original data.
Code:
'----------------------------------------------------------------------------------------------
' Procedure : WildcardMatch
' Author : Jack
' Date : 19/03/2014
' Purpose : To parse a string looking for patternA, then find patternB, concatenate the character
' between patternA and PatterB to the desired output base, as well as any characters beyond PatternB in the input
' string.
'
' ** Parameters **
' sStringToChange ===the original/old code
' sPatternA ===the leftmost / first search pattern
' sPatternB ===the rightmost / second search pattern
' sOutputBase ===the new base to attached parsed characters to
'
' ** The Rule **
'
' Rule:
' Segment??Product* -> Product??*
' NOTE: ***
' *** Here the ?? represents 0 or more characters for our purposes. This is NOT
' *** the ? single character used by M$oft.
' ***
' *************************************************************************************
'
' from https://www.accessforums.net/programming/assigning-wildcard-matches-variable-42411.html#post217910
'---------------------------------------------------------------------------------------
'
Function WildcardMatch(sStringToChange As String, _
sPatternA As String, _
sPatternB As String, _
sOutputBase As String) As String
Dim s1 As String 'incoming string to check
Dim s3 As String 'text between PatternA and PatternB
Dim s4 As String 'text post patternB
Dim myOutputBase As String
Dim sOutput As String
Dim IEndPatA As Integer 'end of PatternA
Dim IEndPatB As Integer 'end of PatternB
Dim iPos1 As Integer
Dim iPos2 As Integer
10 On Error GoTo WildcardMatch_Error
20 myOutputBase = sOutputBase
30 myPatternA = sPatternA
40 myPatternB = sPatternB
50 s1 = sStringToChange
60 iPos1 = InStr(s1, sPatternA)
70 If iPos1 > 0 Then 'found PatternA
80 IEndPatA = iPos1 + Len(sPatternA)
'found PatternA now check for PatternB
90 iPos2 = InStr(s1, sPatternB)
100 If iPos2 > 0 Then 'patternB found
110 If iPos2 = IEndPatA Then 'nothing between PatternA and PatternB
120 s3 = "" 'this ok go to output
130 Else
140 s3 = Mid(s1, IEndPatA, iPos2 - IEndPatA) 'between Patterns
150 End If
160 IEndPatB = iPos2 + Len(sPatternB)
'check if anything is beyond PatternB
170 If Len(s1) > IEndPatB Then
180 s4 = Mid(s1, IEndPatB)
190 Else
200 s4 = ""
210 End If
' Debug.Print "s3 " & s3 'debugging
220 sOutput = myOutputBase & s3 & s4
''''''Debug.Print sOutput 'testing
230 End If '
240 Else
250 sOutput = "" 'nothing to return
260 End If
' return the converted string
'
270 WildcardMatch = sOutput
280 On Error GoTo 0
290 Exit Function
WildcardMatch_Error:
300 MsgBox "Error " & Err.number & " on line " & Erl & " (" & Err.Description & ") in procedure WildcardMatch of Module Module1"
End Function
Here is the test procedure showing how to call the function.
The test results (below) were the result of running each test string.
Code:
Sub testWildCard()
Dim s1 As String
Dim s2 As String
Dim s3 As String
Dim s4 As String
's1 = "testSegmentXYProduct1234duct1234" 'test
's1 = "SegmentProductSurfBoards" 'test
's1 = "SegmentbbbbbbbbProductSurfBoards"
s1 = "ThishasNO pattern and returns an empty string"
s2 = "Segment"
s3 = "Product"
s4 = "Product"
Debug.Print IIf((WildcardMatch(s1, s2, s3, s4) = ""), "NullString Returned", WildcardMatch(s1, s2, s3, s4))
End Sub
Test Results:
Code:
ProductXY1234duct1234
ProductSurfBoards
ProductbbbbbbbbSurfBoards
NullString Returned
Good luck.