Solved

vbscript and regex problem with pattern Extract just words from string

Posted on 2014-10-07
4
269 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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