22

I got this code below that works for single quotes. it finds all the words between the single quotes. but how would I modify the regex to work with double quotes?

keywords is coming from a form post

so

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)
    }
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
user713813
  • 775
  • 1
  • 8
  • 20

4 Answers4

26

You would simply replace the ' with \" and remove the literal to reconstruct it properly.

MatchCollection col = Regex.Matches(keywords, "\\\"(.*?)\\\"");
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • No need to escape `"` in regex. – Kirill Polishchuk Feb 03 '12 at 18:04
  • prefect. and if I wanted to include the quotes in the string? – user713813 Feb 03 '12 at 18:07
  • @user713813: Move the brackets (as well as the _nongreedy_ mark) to the respective ends of the string. – Nuffin Feb 03 '12 at 18:15
  • According to https://regex101.com/ it's necessary to use `""` in C#/.NET (two consecutive double quotes) to match a single double quote. Unlike Python for instance where you'd simply escape the double quote with a backslash. – domjancik Jul 01 '23 at 15:22
  • Thanks for that modernization update @domjancik. When this was answered back in 2012, the backslash escape character was the preferred method. I think at the time the double quote only worked in VB.Net, and I could be wrong about that. – Joel Etherton Jul 02 '23 at 17:11
13

The exact same, but with double quotes in place of single quotes. Double quotes aren't special in a regex pattern. But I usually add something to make sure I'm not spanning accross multiple quoted strings in a single match, and to accomodate double-double quote escapes:

string pattern = @"""([^""]|"""")*""";
// or (same thing):
string pattern = "\"(^\"|\"\")*\"";

Which translates to the literal string

"(^"|"")*"
Joshua Honig
  • 12,925
  • 8
  • 53
  • 75
7

Use this regex:

"(.*?)"

or

"([^"]*)"

In C#:

var pattern = "\"(.*?)\"";

or

var pattern = "\"([^\"]*)\"";
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
6

Do you want to match " or ' ?

in which case you might want to do something like this:

[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);
}
Sam Greenhalgh
  • 5,952
  • 21
  • 37