Solved

vbscript and regex problem with pattern Extract just words from string

Posted on 2014-10-07
4
251 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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.

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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now