Guest User

Untitled

a guest
May 2nd, 2012
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. Regex.Matches c# double quotes
  2. keywords = 'peace "this world" would be "and then" some'
  3.  
  4.  
  5. // Match all quoted fields
  6. MatchCollection col = Regex.Matches(keywords, @"'(.*?)'");
  7.  
  8. // Copy groups to a string[] array
  9. string[] fields = new string[col.Count];
  10. for (int i = 0; i < fields.Length; i++)
  11. {
  12. fields[i] = col[i].Groups[1].Value; // (Index 1 is the first group)
  13. }// Match all quoted fields
  14. MatchCollection col = Regex.Matches(keywords, @"'(.*?)'");
  15.  
  16. // Copy groups to a string[] array
  17. string[] fields = new string[col.Count];
  18. for (int i = 0; i < fields.Length; i++)
  19. {
  20. fields[i] = col[i].Groups[1].Value; // (Index 1 is the first group)
  21. }
  22.  
  23. MatchCollection col = Regex.Matches(keywords, "\"(.*?)\"");
  24.  
  25. string pattern = @"""([^""]|"""")*""";
  26. // or (same thing):
  27. string pattern = ""(^"|"")*"";
  28.  
  29. "(^"|"")*"
  30.  
  31. "(.*?)"
  32.  
  33. "([^"]*)"
  34.  
  35. var pattern = ""(.*?)"";
  36.  
  37. var pattern = ""([^"]*)"";
  38.  
  39. [Test]
  40. public void Test()
  41. {
  42. string input = "peace "this world" would be 'and then' some";
  43. MatchCollection matches = Regex.Matches(input, @"(?<=(['""])).*?(?=1)");
  44. Assert.AreEqual("this world", matches[0].Value);
  45. Assert.AreEqual("and then", matches[1].Value);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment