Solved

Matching multiple strings in RegExp

Posted on 2004-08-19
3
319 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

777 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