Recover text between " in a file with C#

theflamme

New member
Joined
Mar 16, 2005
Messages
1
Hi, (sorry for my english!)

I have a file like this:
"string1","string2","string3","string4"....

I want the string between the " without the " and the ,

In php, my co-worker used this:
preg_match_all(|"([^"]*)",?|i, $content, $matches);

$content = the file

We upgrade in C#
With c# I use something like this:
MatchCollection Matches = Regex.Matches(fiche, "\"([^\"]*)\",?", RegexOptions.IgnoreCase);

I put the result in an array:
for (int i = 0; i < Matches.Count; i++)
{
array.Add(Matches[0].Groups.ToString();
}

I have this result
0: "string1",
1: string1
2: (empty)
3: (empty)
....

But I have the right count of string.

I really dont understand whats wrong.

Thank you
 
Okay... Im not particularly familiar with either c# or regular expressions, so dont yell at me if it doesnt run right, but I whipped out my #develop, my Microsoft Visual Basic .NET, and made this and after a couple minutes of adding forgotten semi-colons and brackets and fixing my casing, I came up with this.

C#:
void example() {
	string filetext  = "\"string1\",\"string2\",\"string3\",\"string4\"";
	MatchCollection Matches = Regex.Matches(filetext, "\"([^\"]*)\",?", RegexOptions.IgnoreCase);
	string[] x = new string[Matches.Count];
	for (int i = 0; i < Matches.Count; i++) {
		x[i] = Matches[i].Groups[1].ToString();
	}
	//x[] now equals an array of the matches without the quotes
}

Dont know why that came out double spaced...
 
Back
Top