Solved

Regex Expression with Excel VBA

Posted on 2015-01-09
7
146 Views
Last Modified: 2015-01-11
How to modify the regex expression in order to handle an additional case, -93ASV-0.59 and break it down into 3 parts:
(1) -93
(2) ASV
(3) -0.59
 
    Set regex = CreateObject("VBScript.RegExp")
    regex.Pattern = "(\d+)([A-Z]+)(\d+.\d+)"
    If regex.test(theString) Then
        Set matches = regex.Execute(theString)
        arr(0) = matches.Item(0).SubMatches(0)
        arr(1) = matches.Item(0).SubMatches(1)
        arr(2) = matches.Item(0).SubMatches(2)
        getPart = arr
    End If

Result
Input  
   11AFD7.69
Output
    arr(0) = 11
    arr(1) = AFD
    arr(2) = 7.69
0
Comment
Question by:AXISHK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
ID: 40541679
"(-?\d+)([A-Z]+)(-?\d+\.\d+)"
0
 

Author Comment

by:AXISHK
ID: 40541832
Is there any reference for Regex expression for VBA ? Is the syntax is the similar as other language ?
0
 

Author Comment

by:AXISHK
ID: 40541858
BTW
What's mean for -?, no need to add \ in front of it ?

http://msdn.microsoft.com/en-us/library/ms974570.aspx

Is there nay reference on the regex usage ? I can't find the meaning for "-"  ? Tks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Assisted Solution

by:Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs) earned 200 total points
ID: 40543423
A dash has no special meaning in Regex so does not need to be escaped with the back-slash.

"-?" is looking for zero or more dashes so that the pattern -?\d+ will match -93 or 93.
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 40543428
A good article on Regex in VBA, written by Excel genius Patrick Matthews, can be found here.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40543437
"-?" is looking for zero or one dashes
"-*" would look for zero or more dashes
"-+" would look for one or more dashes

A dash can have special meaning inside [],  e.g. "[A-Z]"
0
 

Author Closing Comment

by:AXISHK
ID: 40543550
Tks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

726 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