Unexpected Regex match

I'm using the createobject("vbscript.regexp") object in a VBA environment.  I'm getting an extra, unexpected, match that I'd like to eliminate or understand.

pattern: (.*?)($|(?:&#\d+;))
string: Now 15 & $42.0 the time;  for all good# "men" -2 pet the dog cat horse 1,234.56

I'm getting the expected matches (submatch tuples):
("Now 15 & $42.0 the time;  for all good# ", """)
("men", """)
(" -2 pet the dog cat horse 1,234.56", "")

as well as this unexpected match:
("", "")

Why is this happening and is there a better pattern that will eliminate this extra match?
LVL 48
aikimarkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try
 (.+?)($|(?:&#\d+;))

Open in new window

it is not unexpected since .*? can match a zero length string and only the 6 of the 3rd match is "used", not the end of string($)

Regards
0
aikimarkAuthor Commented:
pls try
There's no guarantee that there will be a non-empty string prior in the first capture.  There might be two consecutive &#\d+; sequences or the first &#\d+; sequence might be at the start of the string.
0
Rgonzo1971Commented:
since you cannot be sure then I would loop for the n-1 matches since the last one will always be there
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rgonzo1971Commented:
You could also use

(.*?)(&#\d+;)|(.+$) but then the matching items are different between less than n (1 and 2) and n (3) depending

REgards
0
aikimarkAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.