立即注册 登录
SCMLife.com 返回首页

xn_scmlife的个人空间 http://space.scmlife.com/?8945 [收藏] [复制] [分享] [RSS]

日志

参考:larrysteinle完成去除sql脚本中注释的函数。(正则式)(Beta版)。Thank you,la ...

已有 981 次阅读2011-7-9 16:01


Thank you,larrysteinle!


Original function of the algorithm is relatively clear, but some problems.(Tranlated by google)

改进后的函数(1,所有语句不会显示在一行上,导致在sql文件太大时,一行字符串无法存储;2,原有函数没有跳过已经忽略的字符数iSkipLength )
public static string ToRaw(string commandText)
        {
            RegexOptions regExOptions = (RegexOptions.IgnoreCase | RegexOptions.Multiline);
            string rawText=commandText;
            string regExText = @"('(''|[^'])*')|([\r|\n][\s| ]*[\r|\n])|(--[^\r\n]*)|(/\*[\w\W]*?(?=\*/)\*/)";
            //string regExText = @"('(''|[^'])*')|[\t\r\n]|(--[^\r\n]*)|(/\*[\w\W]*?(?=\*/)\*/)";
             //'Replace Tab, Carriage Return, Line Feed, Single-row Comments and
             //'Multi-row Comments with a space when not included inside a text block.

            MatchCollection patternMatchList = Regex.Matches(rawText, regExText, regExOptions);
            int iSkipLength = 0;
            for (int patternIndex = 0; patternIndex < patternMatchList.Count; patternIndex++)
            {   
                if (!patternMatchList[patternIndex].Value.StartsWith("'") && !patternMatchList[patternIndex].Value.EndsWith("'"))
                {
                    rawText = rawText.Substring(0, patternMatchList[patternIndex].Index - iSkipLength) + " " + rawText.Substring(patternMatchList[patternIndex].Index - iSkipLength + patternMatchList[patternIndex].Length);
                    iSkipLength += (patternMatchList[patternIndex].Length - " ".Length);
                }
            }
            //'Remove extra spacing that is not contained inside text qualifers.
            patternMatchList = Regex.Matches(rawText, "'([^']|'')*'|[ ]{2,}", regExOptions);
            iSkipLength = 0;
            for (int patternIndex = 0; patternIndex < patternMatchList.Count; patternIndex++)
            {
                if (!patternMatchList[patternIndex].Value.StartsWith("'") && !patternMatchList[patternIndex].Value.EndsWith("'"))
                {
                    rawText = rawText.Substring(0, patternMatchList[patternIndex].Index - iSkipLength)+" " + rawText.Substring(patternMatchList[patternIndex].Index - iSkipLength + patternMatchList[patternIndex].Length);
                    iSkipLength += (patternMatchList[patternIndex].Length - " ".Length);
                }
            }
            //'Return value without leading and trailing spaces.
            return rawText.Trim();

        }

==========================================================================
原有函数Thanks for larrysteinle

Public Shared Function ToRaw(ByVal&nbsp;commandText As String) As String
  Dim regExOptions&nbsp;As RegexOptions&nbsp;= regExOptions.IgnoreCase Or regExOptions.Multiline
  Dim rawText&nbsp;As String = commandText
  Dim regExText As String = "('(''|[^'])*')|[\t\r\n]|(--[^\r\n]*)|(/\*[\w\W]*?(?=\*/)\*/)"
  'Replace Tab, Carriage Return, Line Feed, Single-row Comments and
  'Multi-row Comments with a space when not included inside a text block.
  Dim patternMatchList&nbsp;As MatchCollection&nbsp;= Regex.Matches(rawText, regExText, regExOptions)
  For patternIndex&nbsp;As Integer = patternMatchList.Count - 1 To 0 Step -1
    With patternMatchList.Item(patternIndex)
      If Not .Value.StartsWith("'") And Not .Value.EndsWith("'") Then
        rawText&nbsp;= rawText.Substring(0, .Index) & " " & rawText.Substring(.Index + .Length)
      End If
    End With
  Next
  'Remove extra spacing that is not contained inside text qualifers.
  patternMatchList = Regex.Matches(rawText, "'([^']|'')*'|[ ]{2,}", regExOptions)
  For patternIndex As Integer = patternMatchList.Count - 1 To 0 Step -1
    With patternMatchList.Item(patternIndex)
      If Not .Value.StartsWith("'") And Not .Value.EndsWith("'") Then
        rawText = rawText.Substring(0, .Index) & rawText.Substring(.Index + .Length - 1)
      End If
    End With
  Next
  'Return value without leading and trailing spaces.
  Return rawText.Trim
End Function


鸡蛋

鲜花

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

QQ|小黑屋|手机版|无图版|SCMLife.com ( 京ICP备06056490号-1 )

GMT+8, 2022-7-1 09:47 , Processed in 0.042542 second(s), 5 queries , Gzip On, MemCache On.

Powered by SCMLife X3.4 Licensed

© 2001-2017 JoyShare.

返回顶部