excel - Split string in VBA using RegEx - Not Splitting -


really new @ vba/regex have regex function defined

public function splitline(line string) string()  dim regex object set regex = createobject("vbscript.regexp") regex.ignorecase = true regex.global = true  'this pattern matches commas outside quotes 'pattern = ",(?=([^"]"[^"]")(?![^"]"))" regex.pattern = ",(?=([^" & chr(34) & "]" & chr(34) & "[^" & chr(34) & "]" & chr(34) & ")(?![^" & chr(34) & "]" & chr(34) & "))"   splitline = split(regex.replace(line, ";"), ";")  end function 

and reference with:

dim resp string: resp = http.responsetext dim lines variant: lines = split(resp, vblf) dim sline string dim values variant  = 0 ubound(lines)     sline = lines(i)     values = splitline(sline)      stop  next 

this isn't curerntly throwing error - there no split happening.

thanks help!

the local article on regex pick commas outside of quotes has different pattern strings appear successful.

'pattern = /^([^"]|"[^"]*")*?(,)/ regex.pattern = "/^([^" &  chr(34) & "]|" &  chr(34) & "[^" &  chr(34) & "]*" &  chr(34) & ")*?(,)/"  'pattern = /(,)(?=(?:[^"]|"[^"]*")*$)/ regex.pattern = "/(,)(?=(?:[^" &  chr(34) & "]|" &  chr(34) & "[^" &  chr(34) & "]*" &  chr(34) & ")*$)/" 

i suggest less common delimiter split on. chrw(8203) (a zero-length unicode space) common delimiter atoms , like.

splitline = split(regex.replace(line, chrw(8203)), chrw(8203)) 

i've culled working function using yet regex pattern

function stripcommasoutsideofquotedstring(rng range)      dim strpattern string     dim regex object      set regex = createobject("vbscript.regexp")     'pattern is: ,(?=([^"]*"[^"]*")*(?![^"]*"))     strpattern = ",(?=([^" & chr(34) & "]*" & chr(34) & "[^" & chr(34) & "]*" & chr(34) & ")*(?![^" & chr(34) & "]*" & chr(34) & "))"     'debug.print strpattern      regex         .global = true         .pattern = strpattern     end      stripcommasoutsideofquotedstring = split(regex.replace(rng.value, chrw(8203)), chrw(8203)) end function 

the above function can array-entered series of columns receive split values.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -