PDA

View Full Version : RegEx Split String



galileo
07-07-2009, 12:47 AM
Hey guys!

I got a rather straight forward question about the use of RegEx in VBA... My situation: The string (see above) needs to be split and each item, that includes the operators, should be stored into an seperate array item.


String:

[A_B_CCDE] 'Definition' + [A_B_OIED] 'Definition twowords' - [A_B_CDEF] 'Another Definition' %A [A_C_IOWS] 'Last Item Definition'

Match pattern:

\B\s(?=(?:\+|-|%A?|/|\*|COS|SIN))|\s(?=\[)

Result:

1 [A_B_CCDE] 'Definition'
2 +
3 [A_B_OIED] 'Definition twowords'
4 -
5 [A_C_IOWS] 'Last Item Definition'


So far so good. But how do I realize this in VBA using the Microsoft VBScript Regular Expressions 5.5 Lib? My knowledge stops at simple RegEx matches... is there a function for "split_with_regEx" ?


Thanks for your help!
André

Bob Phillips
07-07-2009, 01:51 AM
.Net regular expressions has a Split method, b ut not VBScript.

galileo
07-07-2009, 02:49 AM
Thanks for your answer, xld!

Well, what do you reccon then? I need to perform this split about 800 times per run... looping through the strings and match each position manually via regEx?

Bob Phillips
07-07-2009, 03:11 AM
It seems that you are splitting by space, except where is the A%?

galileo
07-07-2009, 03:42 AM
Nope, due to the mass of arguments and the structure I can't split just by whitespaces... RegEx Split would have saved some performance. Is there no better way in VBA?


%A

\B\s(?=(?:\+|-|%A?|/|\*|COS|SIN))|\s(?=\[)



List of all match items

Array("+", "-", "*", "=", "**", "%", "%A", "COUNT", "DATE", "DELTA", "NDIV0", "NODIM", "NOERR", "SUMCT", "SUMGT", "SUMRT", "TIME", "ABS", "CEIL", "DIV", "EXP", "FLOOR", "FRAC", "LOG", "LOG10", "MAX", "MIN", "MIN0", "MOD", "SIGN", "SQRT", "TRUNC", "ACOS", "ASIN", "ATAN", "COS", "COSH", "SIN", "SINH", "TAN", "TANH", "<", "<=", "<>", "==", ">", ">=", "AND", "LEAF", "NOT", "OR", "XOR")

galileo
07-08-2009, 02:16 AM
The full match is actually

(?-imsx:\B\s(?=(?:\+|-|%A?|/|\*|COS|SIN))|\s(?=\[))

as this just groups and does not capture the group itself. Again, this is Perl5 Syntax, how do I manage this problem in VBA / VBscript?

mdmackillop
07-08-2009, 03:32 AM
Can you provide a few sample formulae on which to test possible solutions? Use Manage Attachments in the Go Advanced reply section.

galileo
07-08-2009, 03:42 AM
Certainly. Attached find some formulae as they appear in practice.