Split string with a text qualifier

DaddyCool

New member
Joined
Jul 1, 2022
Messages
1
Programming Experience
Beginner
Hello All.
This is my first post, I'm new to C#.
I would like to spilt the below string. My delimiter => ";" and each part is in double quotes.
C#:
string myLine = "John Smith"; "A"; "45", "Nearly done; follow up";
My expected output:
C#:
John Smith
A
45
Nearly done; follow up
I tried to go with:
C#:
String[] myResponse= Regex.Split(myLine , ";");
Obviously is not working as intended, and I think I need a Regular Expression to handle it properly. As I mentioned at the beginning, I'm new to C#, so I might be wrong.
Thank you very much in advance
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
This is actually more of a regular expression question, rather than a C# question. You would run into a similar problem with any other language that supports regular expressions, or has regular expression libraries.

Anyway you need to structure the regular expression such that the there is a semicolon or comma surrounded by optional whitespace which is surrounded by quotes. Furthermore you need to tag those quotes as non-capturing.

The alternative is to not use regular expressions. It is do-able with just a few flags and loops. What simplifies the problem is that all your strings are surrounded by only double quotes, and you don't have to worry about any escaped or embedded double quotes.
 
Last edited:
Top Bottom