用正则表达式从一个cs文件中取出
//++--++--++ plugin param 214805 start ++--++--++////++--++--++ plugin param 214805 end   ++--++--++//
中间的代码
其中的214805是自动生成的序号
一个cs文件中可能有好几个这样的方法,怎么才能把他们分别取出来啊
高手们求解,总监要杀人了!!
public bool _DD214805(object sender)
{
//++--++--++ plugin param 214805 start ++--++--++//
string regexStr1 = "{}";
MessageService.ShowMessage("aa");
return true;
//++--++--++ plugin param 214805 end   ++--++--++//
}public bool _DD214806(object sender)
{
//++--++--++ plugin param 214806 start ++--++--++//
string regexStr2="{}"
MessageService.ShowMessage("aa");
return true;
//++--++--++ plugin param 214805 end   ++--++--++//
}

解决方案 »

  1.   

    vs里的正则?有多少要处理的地方,少的话直接搜 plugin param 
      

  2.   

    MatchCollection mc = Regex.Matches(yourcode,@"(?is)(?<=//[^/]+start[^/]+//\s*).+?(?=\s*//[^/]+end[^/]+//)");
    foreach(Match m in mc)
    {
        m.Value;//貌似这就是你要的?
    }如果不和你需要,跟帖描述。
      

  3.   

    或是指定IDpublic static string GetSpecialCodeByID(string yourcode ,int id)
    {
        return Regex.Match(yourcode, @"(?is)(?<=//\D+(" + id.ToString() + @")[^/]+start[^/]+//\s*).+?(?=\s*//\D+\1[^/]+end[^/]+//)").Value;
    }
    //调用
    string result = GetSpecialCodeByID(yourcode, 214805);
      

  4.   

    这个不难吧,直接搜索plugin start

    plugin end就可以实现呀
      

  5.   

    求解能不能把着两行注释也提取出来啊
    //++--++--++ plugin param 214805 start ++--++--++//
    //++--++--++ plugin param 214805 end ++--++--++//
      

  6.   

    public static string GetSpecialCodeByID(string yourcode ,int id)
    {
        return Regex.Match(yourcode, @"(?is)//\D+(" + id.ToString() + @")[^/]+start[^/]+//\s*.+?\s*//\D+\1[^/]+end[^/]+//").Value;
    }
    //调用
    string result = GetSpecialCodeByID(yourcode, 214805);
      

  7.   

    这个正则表达式(?is)(?<=//[^/]+start[^/]+//\s*).+?(?=\s*//[^/]+end[^/]+//)
    在取//++--++--++ plugin param 214805 start ++--++--++//
    //++--++--++ plugin param 214805 end ++--++--++//
    中间的时候,如果是//++--++--++ plugin param 273004 start ++--++--++//
    //MDEntityServerServerClient DM = sender as MDEntityServerServerClient;
    //TApplyUpdateArgs E=e as TApplyUpdateArgs;
    this.Owner.GetTable("Content").OpenEmpty();
    return false;
    //++--++--++ plugin param 273004 end ++--++--++//
    这样的话,取出来为\r
    里面的代码没取到
      

  8.   

    这个正则表达式(?is)(?<=//[^/]+start[^/]+//\s*).+?(?=\s*//[^/]+end[^/]+//)
    在取//++--++--++ plugin param 214805 start ++--++--++//
    //++--++--++ plugin param 214805 end ++--++--++//
    中间的时候,如果是//++--++--++ plugin param 273004 start ++--++--++//
    //MDEntityServerServerClient DM = sender as MDEntityServerServerClient;
    //TApplyUpdateArgs E=e as TApplyUpdateArgs;
    this.Owner.GetTable("Content").OpenEmpty();
    return false;
    //++--++--++ plugin param 273004 end ++--++--++//
    这样的话,取出来为\r
    里面的代码没取到
      

  9.   

    因为捕获到了很多空格,你如果Regex.Matches返回集合,你会在集合最后面找到你实际需要的内容。
    修改一下,按你当前的需要,剔除掉空格的捕获。(?is)(?<=//[^/]+start[^/]+//\s*)(?!\s).+?(?=\s*//[^/]+end[^/]+//)
      

  10.   

    Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation.
    使用Regex.Matches取的时候报这个错误是什么原因啊
      

  11.   

    附上代码,取这段代码的时候public partial class mymda_File__EntityClient : Base.PluginSocket_MDEntityClient
        {
            //public mymda_File__EntityClientDefine Const;
            #region Partial Code
            //++--++--++ partial code 226730 start ++--++--++//
            private static bool IsTimeEquel(DateTime t1, DateTime t2)
            {
                Double TotalMilliseconds = Math.Abs(t1.Subtract(t2).TotalMilliseconds);
                return TotalMilliseconds < 100.0D;
            }
                    private void UploadOneFile(DataRow ParentRow, FileSystemInfo item, DataRow FileRow)
            {
                DataTable ConTable = this.Owner.DTable;
                byte[] bytes = TZip.File2Buffer(item.FullName, System.IO.Compression.CompressionMode.Compress);
                DataRow ContentRow = ConTable.NewRow();
                FileRow["File_Name"] = item.Name;
                ContentRow["File_ID"] = FileRow["File_ID"];
                ContentRow["Content"] = bytes;
                FileRow["FileSize"] = (item as FileInfo).Length;
                FileRow["MDA5"] = TStringHelper.md5_hash(item.FullName);
                FileRow["Parent_ID"] = ParentRow["File_ID"];
                FileRow["IsFile"] = 1;
                FileRow["LastUpdateTime"] = item.LastWriteTime;
                this.Owner.DTable.Rows.Add(ContentRow);
            }
            private void DoDowload(string SelectedPath, DataRow CurrentRow, bool IsShowMessage)
            {
                DownLoadFiles(SelectedPath, CurrentRow);
                this.Owner.DTable.Close();
                PathService.RemoveAbandonFiles(SelectedPath);
                if (IsShowMessage)
                {
                    HintService.HintInfo("文件下载完成!");
                }
            }               
    //++--++--++ partial code 226730 end ++--++--++//        #endregion
            
              }
      

  12.   

    大哥,给你解释下,我要取两个东西,一个是ID,一个是代码,因为我要根据ID去修改数据库里的东西!!
    而且现在有两种类型,一种是partial code,一种是plugin param
      

  13.   

    (?is)(?<=//[^/]+?\D+(\d+)[^/]*?start[^/]+//\s*)(?!\s).+?(?=\s*//[^/]+end[^/]+//)分组1是id
      

  14.   

    不懂你的意思了。现在你要的东西和顶楼要的不同了把?顶楼是给一个id去获取对应的内容。现在要给代码获取id和什么东西?被你弄糊涂了。
      

  15.   

    不懂你的意思了。现在你要的东西和顶楼要的不同了把?顶楼是给一个id去获取对应的内容。现在要给代码获取id和什么东西?被你弄糊涂了。
      

  16.   

    呵呵,大哥不好意思,以前没说明白,现在是这个样子的,给了一个cs文件,从中间获取代码,这个文件中有两种代码要获取,一种是//++--++--++ partial code 226730 start ++--++--++//,一种是//++--++--++ plugin param 226508 start ++--++--++//里面的,然后我要把这些代码写到数据库里去,依据的ID就是注释里面的ID,也就是说我要根据这个ID去把数据库里的CodeText列修改成现在取出来的代码
      

  17.   

    你贴的我保存到一个demo.cs文件中的。
    看这是不是你要的。
    public static void Test()
    {
        string yourcode = File.ReadAllText("demo.cs");
        //调用
        string result = GetSpecialCodeByID(yourcode, 226730);        
        Console.WriteLine(result);
    }public static string GetSpecialCodeByID(string yourcode, int id)
    {
        return Regex.Match(yourcode, @"(?is)(?<=//\D+(" + id.ToString() + @")[^/]+start[^/]+//\s*)(?!\s).+?(?=\s*//\D+\1[^/]+end[^/]+//)").Value;
    }
      

  18.   

    大哥看下这段代码,在Matches的时候,报12楼的错误public const string regStr2 = @"(?is)(?<=//[^/]+start[^/]+//\s*)(?!\s).+?(?=\s*//[^/]+end[^/]+//)";
    public const string regCodeStr = @"(?is)//\D+()[^/]+code[^/]+start[^/]+//\s*(?!\s).+?\s*//\D+\1[^/]+end[^/]+//";
    DirectoryInfo d = new DirectoryInfo(FileName);
                string fileText = File.ReadAllText(FileName);
                Dictionary<int, string> di = new Dictionary<int, string>();
                MatchCollection mc = Regex.Matches(fileText, regCodeStr);
                foreach (Match item in mc)
                {
                    string value = Regex.Match(item.Value, regStr2).Value;
                    int key = Convert.ToInt32(Regex.Match(item.Value, @"(\d+)").Value);
                    di.Add(key, value);
                }
      

  19.   

    大哥,也许是我前面没描述清楚吧,简单点说就是从cs文件中取出注释里ID和代码
      

  20.   

    仔细一点,我相信你能明白如何把我给你的融合到你项目中。你的问题 -> 你的描述 -> 我的理解 - >我的答案 - >你实际的问题 ->你修改我的代码->错误所以,你仔细阅读上面每一次我给你的回复,不要让我反复反复的重写一样的东西了。
      

  21.   


    你反复几次描述都不一样
    最开始是已知id,找对应id的代码段。现在又成了不知道id,找类似的代码段,同时获取id。基本的考试都知道第一步要说明已知条件。
    如果是知道id,知道文本,找代码段。我给你26楼的就是完整例子,你代码放到demo.cs就可以看到结果了。如果是后者。
    19楼就是结果了。
    如何取分组麻烦你看一下msdn,稍微用心一点
    m后面打个点都看得到Groups了。
    我的意思你明白么?首先,你没明确表达自己意思,其次,不论你的几种可能我都给你答案了。出于网友,我做的足够了。对于你,希望你下次准确描述问题,不要浪费他人时间猜测你的问题,而不是解决你的问题,好么?