Ajouve
Ajouve

Reputation: 10089

split on nonalphanumeric vba 2003

I have a string like

Aaa-bbb cccc//DD

I'd like to split tout have an array with

Aaa
bbb
cccc
DD

I think I have tout use regex and split but I dont know how to do with vba 2003

Upvotes: 1

Views: 69

Answers (3)

Johnny Bones
Johnny Bones

Reputation: 8402

It's ugly, but it is tested and workd.

Public Function ArrayTrial()

    Dim arr() As String
    Dim NewArray As String
    Dim MyString As String
    Dim X As Integer
    Dim CurrString As String
    Dim ArrStr As String

    NewArray = ""
    ArrStr = ""
    MyString = "Aaa-bbb cccc//DD"

    For X = 1 To Len(MyString)
      CurrString = Mid(MyString, X, 1)
      If CurrString Like "[a-z.]" Or CurrString Like "[A-Z.]" Or CurrString Like "[0-9.]" Then
        ArrStr = ArrStr & CurrString
      Else
        'Concurrent bogus characters
        If Len(ArrStr) = 0 Then
        Else
          NewArray = NewArray & ArrStr & "|"
        End If
        'MsgBox arr(X)
        ArrStr = ""
      End If
    Next X

    NewArray = NewArray & ArrStr

    arr = Split(NewArray, "|")
End Function

Upvotes: 0

Ron Rosenfeld
Ron Rosenfeld

Reputation: 60174

What application are you running?

Here is an example of VBA coding. You will need to use either early binding or late binding for the regex engine. The code shows the various parameters that are returned for each match.

Dim myRegExp, myMatches, myMatch
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.MultiLine = True
myRegExp.Global = True
myRegExp.Pattern = "\w+"
Set myMatches = myRegExp.Execute(SubjectString)
For Each myMatch In myMatches
    debug.print myMatch.Value
Next myMatch

Upvotes: 3

aelor
aelor

Reputation: 11116

in javascript we do it as

var str ="Aaa-bbb cccc//DD";
var res = str.split(/\W/);
var newArray = [];
for (var i = 0; i < res.length; i++) {
    if (typeof res[i] == 'string' && res[i]) {
        newArray.push(res[i]);
    }
}

console.log(newArray);

Upvotes: 0

Related Questions