theflamme Posted March 16, 2005 Posted March 16, 2005 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 don't understand what's wrong. Thank you Quote
HJB417 Posted March 16, 2005 Posted March 16, 2005 (?s)(?<!\\)(?:\\\\)*"(.*?)(?<!\\)(?:\\\\)*",? Quote
Leaders snarfblam Posted March 16, 2005 Leaders Posted March 16, 2005 Okay... I'm not particularly familiar with either c# or regular expressions, so don't yell at me if it doesn't 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. 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 } Don't know why that came out double spaced... Quote [sIGPIC]e[/sIGPIC]
HJB417 Posted March 16, 2005 Posted March 16, 2005 simplier way is x[i] = Matches[i].Result("$1"); Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.