Solved

vbscript and regex problem with pattern Extract just words from string

Posted on 2014-10-07
4
270 Views
Last Modified: 2014-10-07
vbscript  vba excel 2010

The following code pattern:  works fine in vbscript editor checker:
http://www.regular-expressions.info/vbscriptexample.html



' MY STRING FOR TESTING
cd2 = """*F 3 120E R3*" apea NEAR"""

Dim result As String
Dim cd2 As String
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
' GIVE ME JUST THE WORDS EXCEPT FOR THE QUOTES
RE.Pattern = """\w+[A-Za-z]\s\w+[A-Za-z]"""

RE.Global = True
RE.IgnoreCase = True
Set allMatches = RE.Execute(Cd)

If allMatches.Count <> 0 Then
    result = allMatches.Item(0).SubMatches.Item(0)
End If

cd2 = result


"result" should be  "apea NEAR"


Thanks
fordraiders
0
Comment
Question by:fordraiders
  • 2
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
dsacker earned 500 total points
ID: 40366300
You had a number of syntax errors in your code. Test this code out.
Option Explicit

Dim cd2
Dim allMatches
Dim i
Dim RE

cd2 = """*F 3 test 120E R3*"" apea NEAR"""

Set RE = CreateObject("vbscript.regexp")
' GIVE ME JUST THE WORDS EXCEPT FOR THE QUOTES
RE.Pattern = "\b[A-Za-z]{2,}\b"

RE.Global = True
RE.IgnoreCase = True
Set allMatches = RE.Execute(Cd2)

For i = 0 to (allMatches.Count - 1)
    WScript.Echo "Pattern " & i & " = " & allMatches(i).Value
Next

Open in new window

This will match all words, no matter where they are placed. The "for" loop shows them all.
0
 
LVL 3

Author Comment

by:fordraiders
ID: 40366755
Thanks very much !
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 40367162
thanks
0
 
LVL 20

Expert Comment

by:dsacker
ID: 40367172
Quite welcome.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
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.

680 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