Regex inconsistency with VB6

Viewed 69

The question comes from an update to a quite old program in VB6 (I know what you thing about it, but please forgive me).

I had the old program to look for sentences in a text file beginning with a narrator identifier in the form: L: ", using the Regex pattern [A-Z]: *. Everything was fine for years.

Now I need to add another match for any sentence NOT having a narrator; assuming regX is the Regex object and myMatches the matches collection, I simply test the myMatches.count to see if it >0 and select the alternate path accordingly. The problem is that the next check (without a narrator) after a positive one (where narrator is present) anyway returns a myMatches.count=1 even if this is not true, and the myMatches.value still shows the previous matching.

This even happens when I set regX = nothing and set myMatches=nothing at the end of the test too.

As a side problem, with Set mM As Match the statement: mM = myMatches(1) or mM = myMatches.Item(1) raises an error 'invalid call or wrong argument'.

EDIT
I try to include an example (I replaced the quotes in pattern and txt with a ! to simplify the example):

Dim regX As RegExp
Dim myMatches As MatchCollection
Dim myMatch As Match
Dim patt as string
Dim txt as string

...  

Set regX = New RegExp
regX.IgnoreCase = False
regX.Global = True
patt ="[A-Z]: *!"
txt ="N: !This is a narrator phrase"

regX.Pattern = patt
Set myMatches = regX.Execute(txt)
If myMatches.Count > 0 Then   'narrator
    'I would use: Set myMatch=myMatches(1), but I get an error !, so I must use:'
    For Each myMatch In myMatches 
        ...do something...
    next
end if

patt = "\(.*\)"
txt="Another string, no narrator, no parenthesis)"
regX.Pattern = patt
Set myMatches = regX.Execute(txt)
If myMatches.Count > 0 Then   'this should be false
    'instead the flow comes here since myMatches.Count=1
end if

Hope this is clear enough

0 Answers
Related