2011-01-20 81 views
1

这是半个问题和半个有趣的测验,因为正则表达式会是可笑地复杂并难以创建。如果我自己做(,因为我实际上需要使用它),我会写一个文件解析器而不是正则表达式,尽管我知道在这种情况下可以使用正则表达式,并且我认为也许是一些喜欢挑战的StackOverflow编码人员。这个替换正确的正则表达式是什么?

作为“奖励”,我会将问题保留7天,此时150声誉的赏金将归于具有正确答案的人。我知道回答者的声誉可能大于3K,但代表声望仍然很高。 :)

正则表达式将不得不转向:

[DllImport(EngineDll)] 
public static extern int Graphics(int width, int height, int depth = default(int), int hertz = 60, int flags = (int)(GraphicsBufferType.Back | GraphicsBufferType.Depth)); 

分为:

public static int Graphics(int width, int height, int depth = default(int), int hertz = 60, int flags = (int)(GraphicsBufferType.Back | GraphicsBufferType.Depth)) 
{ 
    if (Engine.ThreadSafe) 
    { 
     lock (typeof(Dll)) 
     { 
      return Dll.Graphics(width, height, depth, hertz, flags); 
     } 
    } 
    else 
    { 
     return Dll.Graphics(width, height, depth, hertz, flags); 
    } 
} 

由于多完全不需要,你可以拥有这一切在1号线,如果你发现它更容易解析:

public static int Graphics(int width, int height, int depth = default(int), int hertz = 60, int flags = (int)(GraphicsBufferType.Back | GraphicsBufferType.Depth)) { if (Engine.ThreadSafe) { lock (typeof(Dll)) { return Dll.Graphics(width, height, depth, hertz, flags); } } else { return Dll.Graphics(width, height, depth, hertz, flags); } } 

现在,如果它不够明显,什么变量是返回类型,方法名称,参数类型,参数名称,参数是否具有默认值,在这种情况下是缺省值。该函数可能是一个void,在这种情况下,不应该有return语句。

根据要求:第二输入 - 输出:

[DllImport(EngineDll)] 
public static extern void EndRender(); 

输出:

public static void EndRender() 
{ 
    if (Engine.ThreadSafe) 
    { 
     lock (typeof(Dll)) 
     { 
      Dll.EndRender(); 
     } 
    } 
    else 
    { 
     Dll.EndRender(); 
    } 
} 

再次,1-衬垫接受。

祝你好运! :)

请注意所有可能会说我只是懒惰的人:改变问题。

+0

我添加了`fun`和`code-bowling`标签,因为如你所说这是一项挑战,并不是真正适合这项工作的工具。但它看起来有趣......哦,为了清楚起见,你想要取代什么?转型的规则是什么?你只是期望参数名称添加到回调? – ircmaxell 2011-01-20 03:24:33

+0

@ircmaxell:我不明白你的问题,对不起。谨慎阐述? – Lazlo 2011-01-20 03:28:16

+0

所以你想要的基础知识是从源移动到目的地的类名,输入参数列表已移动,并且附加到两个返回的参数名称正确? – ircmaxell 2011-01-20 03:30:15

回答

1

对于真正的正则表达式,你不能这么做,因为你需要能够在默认值中处理平衡括号。这就是说,有与扩展,允许你来处理这样的事情正则表达式方言,让我们让球滚动:

s/[DllImport(EngineDll)]\npublic\ static\ extern\ #boilerplate 
    (?<method> #capturing group for the method signature 
    (?<return>\w+)\ #Method return type 
    (?<name>\w+)\(#Method name 
    (?<parameter>\w+\ #parameter type 
    (?<parname>\w+)\ #parameter name 
    (=\ (?&DEFAULTVALUEREGEX))? #optional default value 
    (?{$params = $+{parname}}) #Ok, so this isn't even pretending to be a regex 
           # anymore. If anyone has any better ideas... 
)(?:,\ #parameter seperator 
    \w+\ (?<par>\w+)\ (=\ (?&DEFAULTVALUEREGEX))? 
    (?{ $params .= ", " . $+{par}}))* # more parameters 
    \)); # boilerplate 
/
    public static $+{method} { if (Engine.ThreadSafe) { lock (typeof(Dll)) { return Dll.$+{name}($params); } } else { return Dll.$+{name}($params); }} 
    /x 

我没有包括DEFAULTVALUEREGEX的定义,因为这基本上是需要你来解析任何一种有效的C#表达式,这将足以掩盖其余的逻辑。它也使用了(?{code})构造,使得它甚至可以算作一种正则表达式,这是非常令人怀疑的。另外我还没有测试过它,因为我很懒,它很脆弱(它会破坏一堆仍然有效的C#)。

如果任何人有任何改进,随时编辑它们 - 这就是为什么它是CWiki的第一位。

0

那么,这有什么问题?

s/.*/public static int Graphics(int width, int height, int depth = default(int), int hertz = 60, int flags = (int)(GraphicsBufferType.Back | GraphicsBufferType.Depth)) { if (Engine.ThreadSafe) { lock (typeof(Dll)) { return Dll.Graphics(width, height, depth, hertz, flags); } } else { return Dll.Graphics(width, height, depth, hertz, flags); } }/; 

由于它(可能)是不是你脑子里的东西,必须有另一个转变,同样的正则表达式也有做的事 - 我另有规定什么是工作相当充足。