用正则表达式从一个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 ++--++--++//
}
//++--++--++ 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 ++--++--++//
}
foreach(Match m in mc)
{
m.Value;//貌似这就是你要的?
}如果不和你需要,跟帖描述。
{
return Regex.Match(yourcode, @"(?is)(?<=//\D+(" + id.ToString() + @")[^/]+start[^/]+//\s*).+?(?=\s*//\D+\1[^/]+end[^/]+//)").Value;
}
//调用
string result = GetSpecialCodeByID(yourcode, 214805);
与
plugin end就可以实现呀
//++--++--++ plugin param 214805 start ++--++--++//
//++--++--++ plugin param 214805 end ++--++--++//
{
return Regex.Match(yourcode, @"(?is)//\D+(" + id.ToString() + @")[^/]+start[^/]+//\s*.+?\s*//\D+\1[^/]+end[^/]+//").Value;
}
//调用
string result = GetSpecialCodeByID(yourcode, 214805);
在取//++--++--++ 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
里面的代码没取到
在取//++--++--++ 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
里面的代码没取到
修改一下,按你当前的需要,剔除掉空格的捕获。(?is)(?<=//[^/]+start[^/]+//\s*)(?!\s).+?(?=\s*//[^/]+end[^/]+//)
使用Regex.Matches取的时候报这个错误是什么原因啊
{
//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
}
而且现在有两种类型,一种是partial code,一种是plugin param
看这是不是你要的。
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;
}
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);
}
你反复几次描述都不一样
最开始是已知id,找对应id的代码段。现在又成了不知道id,找类似的代码段,同时获取id。基本的考试都知道第一步要说明已知条件。
如果是知道id,知道文本,找代码段。我给你26楼的就是完整例子,你代码放到demo.cs就可以看到结果了。如果是后者。
19楼就是结果了。
如何取分组麻烦你看一下msdn,稍微用心一点
m后面打个点都看得到Groups了。
我的意思你明白么?首先,你没明确表达自己意思,其次,不论你的几种可能我都给你答案了。出于网友,我做的足够了。对于你,希望你下次准确描述问题,不要浪费他人时间猜测你的问题,而不是解决你的问题,好么?