求一个正则表达式:
输入字符串:
fdskjfklsdjjlfksdjdfskl<%   @TableOwner  :  'dbo'  %  >dfjlskajlfdjsla
fsdjflksdj输出字符串:
<%@TableOwner:'dbo'%>实在感谢,用来做为模版字符串的转义属性。
请高手帮忙,在线等,谢谢!!

解决方案 »

  1.   

    贴段微软写的
     /// <summary>
            /// Method to make sure that user's inputs are not malicious
            /// </summary>
            /// <param name="text">User's Input</param>
            /// <param name="maxLength">Maximum length of input</param>
            /// <returns>The cleaned up version of the input</returns>
            public static string InputText(string text, int maxLength)
            {
                text = text.Trim();
                if (string.IsNullOrEmpty(text))
                    return string.Empty;
                if (text.Length > maxLength)
                    text = text.Substring(0, maxLength);
                text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces
                text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
                text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //&nbsp;
                text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
                text = text.Replace("'", "''");
                return text;
            }
      

  2.   

    输入:
    fdskjfklsdjjlfksdjdfskl<%   @TableOwner  :  'dbo'  %  >dfjlskajlfdjsla
    fsdjflksdjfdskjfklsdjjlfksdjdfskl<%   @TableName  :  'Demo'  %  >dfjlskajlfdjsla
    fsdjflksdj输出数组:用hashtable也都OK
    <%@TableOwner:'dbo'%>
    <%@TableName:'Demo'%>谢谢!
      

  3.   

    MatchCollection mtc=Regex.Matches(input,"<\s*%[\s\S]*?%\s*>");
      

  4.   

    MatchCollection mtc=Regex.Matches(input,"<\s*%[\s\S]*?%\s*>");
    具体说下是什么意思啊,
    ,"<\s*%[\s\S]*?%\s*>");
      

  5.   

    <\s*%[\s\S]*?%\s*> will match
    <
    Any whitespace character 
    * (zero or more times)
    %
    Any character in "\s\S"
    * (zero or more times) (non-greedy)
    %
    Any whitespace character 
    * (zero or more times)
    >