Solved

Regex Expression with Excel VBA

Posted on 2015-01-09
7
136 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Macro Capitalize 4 53
Child Form in front 4 37
Excel for Mac - How make those Tabs larger? 2 31
Excel case statements 3 24
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 …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now