且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Regex.Matches C#双引号

更新时间:2023-02-20 08:10:58

您将只需更换 \键,删除文字适当重建它。

  MatchCollection COL = Regex.Matches(?关键词,\\\(*)\\\);
 

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)
    }

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

MatchCollection col = Regex.Matches(keywords, "\\\"(.*?)\\\"");