?
Solved

vbscript and regex problem with pattern Extract just words from string

Posted on 2014-10-07
4
Medium Priority
?
283 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

752 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