1

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?

7
  • 3
    This looks like you're trying to consume a CSV document. I recommend using a library for this (cf. secretgeek.net/csv_trouble.asp) Commented Oct 19, 2011 at 17:37
  • 2
    You have a problem. "I'll use a regex to solve it!" Now you have two problems. Commented Oct 19, 2011 at 17:41
  • I don't think regex is the way to go about this. I would iterate over the string looking for opening and closing quotes. Commented Oct 19, 2011 at 17:43
  • @JesseSeger: I wouldn't, because what if he has a string that's got an escaped-out quote? "abc\"123" would cause that iteration to fail. I'd follow Factor Mystic's advice and use a CSV parsing library. Commented Oct 19, 2011 at 17:46
  • 1
    @Jesse even if it is homework, I don't see much problem with it - the whole point of this or almost ANY online forum is to help someone get an answer to a problem they're having - that's the way it works in real life, and school is SUPPOSED to be getting you ready for real life, right? Commented Oct 19, 2011 at 19:25

3 Answers 3

2

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect solution. Works very well. Thanks.
2

I think you should use

"(.*?)",?

regex

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

2 Comments

Won't find escaped quotes and it won't ignore inner commas.
Mostly works, but as mentioned by Jesse, is not completely fool proof. But thanks for the answer.
0
(?:"((?:[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...

Comments

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.