Solved

vbscript and regex problem with pattern Extract just words from string

Posted on 2014-10-07
4
273 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
[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
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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…

740 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