?
Solved

Regex Expression with Excel VBA

Posted on 2015-01-09
7
Medium Priority
?
165 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 1200 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 800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
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…

762 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