Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# using regex to parse string between quotes

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.

like image 711
maddo7 Avatar asked Jun 21 '26 05:06

maddo7


2 Answers

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);
}
like image 166
Simon Whitehead Avatar answered Jun 23 '26 18:06

Simon Whitehead


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.

like image 33
Matthew Avatar answered Jun 23 '26 17:06

Matthew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!