[英]What is this piece of code doing with RegEx
一直在研究示例源代码,但我听不懂这部分,这段代码在做什么? 正则表达式部分...
在使用的参数中,“ code”是一个字符串,它是我们传入的C#源代码。
Match m = null;
if ((m = Regex.Match(code, "(?ims)^[/']{2}REFDLL (?<ref>.+?)$")).Success)
{
foreach (string refDll in m.Groups["ref"].Value.Split(new char[] { ';', ',' }))
{
//2008-06-18 by Jeffrey, remove redundant \r
string mdfyRefDll = refDll.Replace("\r", "").Replace("\n", "");
//trim the ending .dll if exists
if (mdfyRefDll.ToLower().EndsWith(".dll"))
mdfyRefDll = mdfyRefDll.Substring(0, mdfyRefDll.Length - 4);
string lcRefDll = mdfyRefDll.ToLower();
if (lcRefDll == "system.data.linq" || lcRefDll == "system"
|| lcRefDll == "system.xml.linq" || lcRefDll == "system.core")
continue;
cp.ReferencedAssemblies.Add(mdfyRefDll + ".dll");
}
}
我认为这张图片可以解决您发布的代码中发生的事情:
Mini C#Lab的项目描述如下:
一个用于简单的简短C#代码运行和测试的便捷工具,您可以节省等待Visual Studio启动的时间,并避免创建大量的一次性项目文件。
似乎该项目缺少文档,因此很难推断出代码的作者为什么在已经存在using指令的情况下选择特定方式添加引用的DLL。 也许他这样做是为了避免与using语句冲突。
首先, (?ims)
指定选项。 i
触发不区分大小写, m
指定多行模式,而s
(IIRC)启用点全部选项,这表示通配符.
包括换行符。
here, with no preceding characters..." while the $
at the end asserts, "The string must here, with no following characters." 然后, ^
断言:“字符串必须在此 ,没有任何前面的字符...”,而末尾的$
断言,“字符串必须在此 ,没有后面的字符。”
[/']{2}
与斜杠或单引号字符中的两个完全匹配,即//
, ''
, /'
和'/
。
REFDLL
与您所看到的完全匹配。
(?<ref>.+?)
与所有其余字符匹配(不需要最后一个问号),并且请记住,由于使用s
选项,其中包括换行符。 这部分存储在名为ref
的匹配项中。
总而言之,它正在尝试匹配类似
//REFDLL helloworld foobar
并将“ helloworld foobar”存储在ref
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.