• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 66
  • Last Modified:

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?
0
aikimark
Asked:
aikimark
  • 3
  • 2
2 Solutions
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now