0

I want to split by commas that are not within parentheses in Excel VBA.

I need VBA script for split string

Ex:

myString = "ab, cd(c1,c2,d1), ef, gh ,ij(i1,i2,j1,j2)"

E need output as below :

[ 
  ab,
  cd(c1,c2,d1),
  ef,
  gh,
  ij(i1,i2,j1,j2) 
]
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
dr. strange
  • 665
  • 7
  • 22
  • @braX, i need answer in VBA – dr. strange Nov 14 '19 at 06:18
  • 1
    And we need to see the code you have tried so far because this is no free code writing service. Please read [No attempt was made](http://idownvotedbecau.se/noattempt/) and [edit] your question. – Pᴇʜ Nov 14 '19 at 07:40

1 Answers1

0

Assuming your parentheses are always singly-nested, splitting on the following regex should work:

,(?![^(]*\))

This uses a negative lookahead which asserts that we don't split on a comma after which follows a ) without first encountering an (, which means another group is being opened.

I don't know if VBA would support lookaheads in its regex though. Check the demo below to see the pattern working.

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360