实现一个简单的C#代码解析器

时间:2013-08-31 02:03:50

标签: c# .net

我的程序应该在源文本框中找到Console.WriteLine("并开始在"之后读取字符串,直到找到"),然后将捕获的字符串存储在变量中。例如,如果输入为:

Console.WriteLine("Hello World")

然后变量的值应为Hello World

帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

string yourInput = ...
var result = Regex.Match(yourInput, "Console.WriteLine[(]\"(.*?)(?<!\\\\)\"[)]").Groups[1].Value;

一点点测试:

string yourInput = "Console.WriteLine(\"\\\") Yeahhh\")"; 
yourInput += "Console.WriteLine(\"At least Regex can handle \"this\"\")";
yourInput += "Console.WriteLine(\"Although \"Regex\" is afraid of parsing \"text\" with nested elements\")";
var matches = Regex.Matches(yourInput, "Console.WriteLine[(]\"(.*?)(?<!\\\\)\"[)]");
foreach (Match m in matches)
    System.Diagnostics.Debug.Print(m.Groups[1].Value);

输出

\") Yeahhh
At least Regex can handle "this"
Although "Regex" is afraid of parsing "text" with nested elements