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
[TABLE="width: 400"]
<TBODY>[TR]
[TD]1
[/TD]
[TD]6SR42020DA330EJ0-Z
[/TD]
[TD]V06+G47+K69+M08+N35+P31+K31
[/TD]
[/TR]
</TBODY>[/TABLE]

Sheet 2
[TABLE="width: 650"]
<TBODY>[TR]
[TD][TABLE="width: 200"]
<TBODY>[TR]
[TD]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>
[/TD]
[/TR]
</TBODY>[/TABLE]
[/TD]
[TD][TABLE="width: 81"]
<TBODY>[TR]
[TD]!K73</SPAN>
[/TD]
[/TR]
</TBODY>[/TABLE]
[/TD]
[TD][TABLE="width: 75"]
<TBODY>[TR]
[TD]!M67</SPAN>
[/TD]
[/TR]
</TBODY>[/TABLE]
[/TD]
[TD][TABLE="width: 53"]
<TBODY>[TR]
[TD]U55</SPAN>
[/TD]
[/TR]
</TBODY>[/TABLE]
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</TBODY>[/TABLE]

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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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,223,275
Messages
6,171,123
Members
452,381
Latest member
Nova88

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