
Untitled
By: a guest on
May 2nd, 2012 | syntax:
None | size: 1.22 KB | hits: 10 | expires: Never
Regex.Matches c# double quotes
keywords = 'peace "this world" would be "and then" some'
// Match all quoted fields
MatchCollection col = Regex.Matches(keywords, @"'(.*?)'");
// Copy groups to a string[] array
string[] fields = new string[col.Count];
for (int i = 0; i < fields.Length; i++)
{
fields[i] = col[i].Groups[1].Value; // (Index 1 is the first group)
}// Match all quoted fields
MatchCollection col = Regex.Matches(keywords, @"'(.*?)'");
// Copy groups to a string[] array
string[] fields = new string[col.Count];
for (int i = 0; i < fields.Length; i++)
{
fields[i] = col[i].Groups[1].Value; // (Index 1 is the first group)
}
MatchCollection col = Regex.Matches(keywords, "\"(.*?)\"");
string pattern = @"""([^""]|"""")*""";
// or (same thing):
string pattern = ""(^"|"")*"";
"(^"|"")*"
"(.*?)"
"([^"]*)"
var pattern = ""(.*?)"";
var pattern = ""([^"]*)"";
[Test]
public void Test()
{
string input = "peace "this world" would be 'and then' some";
MatchCollection matches = Regex.Matches(input, @"(?<=(['""])).*?(?=1)");
Assert.AreEqual("this world", matches[0].Value);
Assert.AreEqual("and then", matches[1].Value);
}