VBA: RegEx & Lookahead

Converemon

New Member
Joined
May 23, 2014
Messages
15
I desperately need a pro to help me deal with some regular expressions and lookaheads. I'm really not sure what the problem is with my code, but when I tell it to search through and see if, say for example, U55 is in the row it returns True when it should be False. All criteria in a row must be True for the function to be True. There will be 6 rows (1 for MLFB expression and 5 for option codes). Any number of the option code columns can be blank. I will post an example, my code, and the results from the Immediate window when testing. If more information is needed, let me know.

Example:
Sheet 1
1
6SR42020DA330EJ0-Z
V06+G47+K69+M08+N35+P31+K31

<TBODY>
</TBODY>

Sheet 2
6SR42020.[ABCD]...[AB].0-Z|6SR42020.[ABCD](320|330|340|345|350|360|370|380)E.0-Z|6SR42020[ABCDEFGHJK][ABCD]387E.0-Z|6SR42020.[ABCD](320|330|340|345|350|360|370)F.0-Z|6SR42020[ABCDEFGHJK][ABCD](380|387|410|411)F.0-Z</SPAN>

<TBODY>
</TBODY>
!K73</SPAN>

<TBODY>
</TBODY>
!M67</SPAN>

<TBODY>
</TBODY>
U55</SPAN>

<TBODY>
</TBODY>

<TBODY>
</TBODY>

This should equal False because of the U55 but it is returning True. "!" means Not.

Code:
Code:
Function Test_MLFB_OPTS(sConfiguration, sMLFBm, sOpt1m, sOpt2m, sOpt3m, sOpt4m, sOpt5m) As Boolean
Set re = New RegExp
re.IgnoreCase = False
re.Pattern = f_Lookahead(sOpt5m) & f_Lookahead(sOpt4m) & f_Lookahead(sOpt3m) & f_Lookahead(sOpt2m) & f_Lookahead(sOpt1m) & Replace(sMLFBm, "…", "...")
Test_MLFB_OPTS = re.Test(sConfiguration)
End Function
Function f_Lookahead(sOpt_mask) As String
    If sOpt_mask = "" Then
        f_Lookahead = ""
    ElseIf Left(sOpt_mask, 1) = "!" Then 'negative lookahead assertion
        f_Lookahead = "(?!.*" & Right(sOpt_mask, Len(sOpt_mask) - 1) & ")"
    Else 'positive lookahead assertion
        f_Lookahead = "(?=.*" & sOpt_mask & ")"
    End If
End Function

bApplies = Test_MLFB_OPTS(curMLFBCell.Value & curoptlistcell.Value, wsSBoM.Cells(iSBoMRow, imlfbmask).Value, wsSBoM.Cells(iSBoMRow, _
                    iOpt1Mask).Value, wsSBoM.Cells(iSBoMRow, iOpt2Mask).Value, wsSBoM.Cells(iSBoMRow, iOpt3Mask).Value, wsSBoM.Cells(iSBoMRow, _
                    iOpt4Mask).Value, wsSBoM.Cells(iSBoMRow, iOpt5Mask).Value)

If bApplies
    'Do something
End If

Results:

?re.Test(sConfiguration)
True
?re.Pattern
(?!.*U55)(?!.*M67)(?!.*K73)6SR42020.[ABCD]...[AB].0-Z|6SR42020.[ABCD](320|330|340|345|350|360|370|380)E.0-Z|6SR42020[ABCDEFGHJK][ABCD]387E.0-Z|6SR42020.[ABCD](320|330|340|345|350|360|370)F.0-Z|6SR42020[ABCDEFGHJK][ABCD](380|387|410|411)F.0-Z
?sConfiguration
6SR42020DA330EJ0-ZV06+G47+K69+M08+N35+P31+K31

Thank you very much for your time.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
For anybody with a similar problem in the future, I had to put "(" and ")" around the expressions with the OR "|".
 
Upvote 0

Forum statistics

Threads
1,216,105
Messages
6,128,859
Members
449,472
Latest member
ebc9

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top