Solved

Matching multiple strings in RegExp

Posted on 2004-08-19
3
320 Views
Last Modified: 2008-02-26
I want to use VB rebexp regular expressions object to match multiple strings with different structure.  e.g. In "37g" I want to match "37" as one string and "g".  I tried:

  objRE.Pattern = "([0-9]+)([a-z]+)"
  objRE.Global = True
  Set objMatches = objRE.Execute(SizeString)

but objMatches.count always comes back as 1 and the match is "37g"

I thought that by using two sets of brackets I should get two matches in the objMatches collection (that's what it implies on MSDN: "Matches pattern and remembers the match. The matched substring can be retrieved from the resulting Matches collection, using Item [0]...[n]. To match parentheses characters ( ), use "\(" or "\)".")

Dan
0
Comment
Question by:danpid
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mvidas earned 150 total points
ID: 11840413
Hi Dan,
you could try the | symbol, meaning OR in regexp.

objRE.Pattern = "([0-9]+)|([a-z]+)"

objRE.Pattern = "\d{2}|[a-z]{1}"      will also have the same affect, will consider a match to be either 2 digits or one lower case letter

Matt
0
 
LVL 1

Author Comment

by:danpid
ID: 11840678
Thanks Matt.  VB seems to have a different approach to regex to that which I am used to (from perl etc).

\d is handy too
0
 
LVL 35

Expert Comment

by:mvidas
ID: 11841139
Not a problem, I'm used to it in VBA for excel, but its the same thing.  Thanks for the grade!
Matt
0

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question