2

I have a row of cells in Excel that follow the following syntax:

randomtext VALUES (randomnumber, randomtext,

I have a VB script to split the text using regular expressions:

Public Function SplitRe(text As String, pattern As String, Optional ignorecase As Boolean) As String()
  Static re As Object
  If re Is Nothing Then
    Set re = CreateObject("VBScript.RegExp")
    re.Global = True
    re.MultiLine = True
  End If
  re.ignorecase = ignorecase
  re.pattern = pattern
  SplitRe = Strings.Split(re.Replace(text, vbNullChar), vbNullChar)
End Function

The delimiter I'm using: (VALUES)+(\s+)+(.)+\d+(,)

This results in the text being split as:

randomtext

What I'm trying to achieve is to split the text after the script finds the delimiter, creating this:

randomtext VALUES (randomnumber,

But I can not find a way to properly augment my script to do so. Anyone see a solution?

2
  • Like your use of VBScript.RegExp as a Static object. Can you add sample data together with expected results? Commented Aug 17, 2017 at 14:25
  • Unless I'm missing something, you don't need RE for this, you could use Split? Commented Aug 17, 2017 at 14:27

1 Answer 1

1

Without regular expressions neither splitting:

Faster:

 Left(text, InStrRev(text,",")) 'Truncate string after last comma

Safer (assuming VALUES ( doesn't occur in first randomtext):

 Left(text, InStr(InStr(text,"VALUES ("),text,",")) 'Truncate string after comma which is after "VALUE ("
Sign up to request clarification or add additional context in comments.

6 Comments

The randomtext before VALUES differs per row, and contains symbols '(' and ',' That's why I wanted to play it safe and use regex
and how would a your regex process "VALUE (" VALUE (1.45, "some text"?
I like your Left(text, InStr(InStr(text,"VALUES ("),",")) solution, but somehow it's not giving me output.
Is VALUES case sensitive and space is regular space?
Case sensitive and regular space, yes
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.