2

I'm fairly new and I couldn't get this to work properly.

I have this string

["string1","string2","string3","string4","string5","string6","string7","string8","string9","string10"]

And I want to get all values between the "

I think regex would be best to do the task.

Thanks for your help.

5

4 Answers 4

5

This will capture between the quotes:

(?<=")[\w]+(?!=")

An expanded example:

string s = "[\"string1\",\"string2\",\"string3\",\"string4\",\"string5\",\"string6\",\"string7\",\"string8\",\"string9\",\"string10\"]";

foreach (Match m in Regex.Matches(s, "(?<=\")[\\w]+(?!=\")")) {
    Console.WriteLine(m.Value);
}
6
  • OP, this is a better answer than the spliting version. Aug 10, 2012 at 13:44
  • @RyanCopley could you explain why you think it's better than splitting the string? IMO using a regex for simple string manipulation like this is overkill. It's also more effecient to use a split than it is to use a regex...
    – James
    Aug 10, 2012 at 13:48
  • 1
    Efficiency is debatable for a single use scenario.. but then we are speculating :) I hope it helped in some way anyway :) Aug 10, 2012 at 14:11
  • 1
    @SimonWhitehead True, however, a regex should be used for matching data, not parsing.
    – James
    Aug 10, 2012 at 14:17
  • 1
    Could you please improve it so it also works with spaces within the quotes?
    – maddo7
    Aug 10, 2012 at 21:33
3

Since this looks like JSON, try using the JavaScriptSerializer class

string myString = "[\"string1\",\"string2\",\"string3\",\"string4\",\"string5\",\"string6\",\"string7\",\"string8\",\"string9\",\"string10\"]";

string[] strings = (new JavaScriptSerializer()).Deserialize<string[]>(myString);

foreach (string str in strings)
{
    Console.WriteLine(str);
}

Kinda seems overkill though.

1

In my opinion, this is a job for Split rather than just Regex:

string str = "[\"string1\",\"string2\",\"string3\",\"string4\",\"string5\",\"string6\",\"string7\",\"string8\",\"string9\",\"string10\"]";
Regex rgx = new Regex("[\\[\\]\"]"); // get rid of the quotes and braces
str = rgx.Replace(str,""); 
string [] split = str.Split(','); // split on commas. that's it.

foreach (string s in split)
{
    Console.WriteLine(s);
}

This requires no special matching regex that you may to change if your quoted strings get messy. Consequently, it is (again, in my opinion) more elegant.

0

If you mean you have a CSV string e.g.

"\"string1\", \"string2\", \"string3\"" 

Then you don't need a regex for something as trivial as this, you could use String.Split with a sprinkle of LINQ:

var values = csvString.Split(',').Select(s => s.Replace("\"", "").Trim());
2
  • you won't be able to do this if the quoted string contains a comma.
    – alltej
    Jun 20, 2017 at 19:33
  • @alltej I didn't say it was a universal solution, it was one that would work for the given sample data :)
    – James
    Jun 22, 2017 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.