有一个sql查询语句 select * from User where name=' ' and compary='#$%%$##'
我想查找所有单引号里面的东西,就比如这个name=' '中' ',compary='#$%%$##'中'#$%%$##',单引号里面可能有各种字符串,也有可能是空。
请问这个这个正则表达式怎么写?

解决方案 »

  1.   


    void Main()
    {
        string s=@" select * from User where name=' ' and compary='#$%%$##'";
       foreach(Match m in Regex.Matches(s,@"(?<==').*?[^'](?=')"))
       {
       Console.WriteLine(m.Value);
       }
    }/*
     
    #$%%$##
    */
      

  2.   

    MatchCollection mc = Regex.Matches("", @"(?<=')[^']*(?=')");
    foreach (Match m in mc)  
    {    
      TextBox1.Text += m.Value + "\n";  
    }
      

  3.   

    MatchCollection mc = Regex.Matches("", @"(?<=')[^']*(?=')");
    foreach (Match m in mc)  
    {    
      TextBox1.Text += m.Value + "\n";  
    }
      

  4.   

    MatchCollection mc = Regex.Matches("", @"(?<=')[^']*(?=')");
    foreach (Match m in mc)  
    {    
      TextBox1.Text += m.Value + "\n";  
    }
      

  5.   

     select * from User where name=' ' and compary='#$%%$##';
    foreach(Match m in Regex.Matches(s,@"(?<=').*?[^'](?=')"))
       {
       //m为匹配后的结果可以对匹配的结果操作
       }
      

  6.   

    string sql = "select * from User where name=' ' and compary='#$%%$##'";
    Dictionary<string, string> condition = new Dictionary<string, string>();
    Match m = Regex.Match(sql, @"where(\s*(?:and\s*)?(\S*)\s*=\s*(')?((?:(?!\3).)+)\3)+");
    for (int i = 0; i < m.Groups[1].Captures.Count; i++)
    {
        condition.Add(m.Groups[2].Value, m.Groups[4].Value);
    }
    condition//就是你要的结果
      

  7.   

                string pattern = "(?<!')'((?:[^']|'')*?)'(?!')";
                string sql = "select * from User where name=' ' and compary='#$%%$##' and xxx='abcd''efg'";
                foreach (Match m in Regex.Matches(sql, pattern))
                {
                    Console.WriteLine("【{0}】", m.Groups[1].Value);
                }
      

  8.   

    上述正则只对正确的SQL语句有效,如果是无规则的字符串的话就没这么简单了。。