adnan sami
adnan sami

Reputation: 15

Regex split a string

I am at my wit ends (being a regex novice). I need to split a string like this

"abc","","av,as","hello world","nice,name"

into

'abc'
'\blank\'
'av,as'
'hello world'
'nice,name'

Using c# or excel vbs, can someone help with the regex expression?

Upvotes: 1

Views: 231

Answers (3)

Code Jockey
Code Jockey

Reputation: 6721

Fairly straightforward:

"(\\.|[^"\\])*"

will work as shown:

Comma-delimited list of quoted strings

It will allow escaped quotes and possible whitespace between quotations, and is POSIX compliant, should you ever need that!

EDIT

I should probably note that it will basically NOT be possible to get the '\blank\' you specified directly from the regex engine, but would be relatively trivial to get it from code that checks the the length of the match and replaces it if is less than three characters long (as the match will be "" if there was an empty string)

END EDIT

Please ask if you would like me to break down the expression!

Upvotes: 2

Ben
Ben

Reputation: 13645

(?:"((?:[a-z])+(?:[ ,a-z]+))")?("")? 

can be used

if group 1 and 2 are emtpy, you matched a , if group 1 has a value you matched a string and if group 2 has a value you matched emtpy double quotes ""

But yes as said, you should use a parser for speed and accuracy...

Upvotes: 0

Pavlo Vasylchenko
Pavlo Vasylchenko

Reputation: 36

I think you should use

"(.*?)",?

regex

Example: http://regexr.com?2uvk8

Upvotes: 2

Related Questions