实现一个简单的C#代码解析器
我的程序应该在源文本框中找到实现一个简单的C#代码解析器,c#,.net,C#,.net,我的程序应该在源文本框中找到控制台.WriteLine(),并开始读取“”后面的字符串,直到找到”,然后将捕获的字符串存储在变量中。例如,如果输入为: Console.WriteLine("Hello World") 那么变量的值应该是Hello World 非常感谢您的帮助。正确编辑您的问题。@NikhilAgrawal我知道了这可能对您很有用:String.IndexOf可能足以完成所描述的任务……但从标题中,您需要了解更多信息-您需要清楚地了解您希望在解析方面走多远,以及您愿意使用/您的
控制台.WriteLine(
),并开始读取“
”后面的字符串,直到找到”
,然后将捕获的字符串存储在变量中。例如,如果输入为:
Console.WriteLine("Hello World")
那么变量的值应该是Hello World
非常感谢您的帮助。正确编辑您的问题。@NikhilAgrawal我知道了这可能对您很有用:
String.IndexOf
可能足以完成所描述的任务……但从标题中,您需要了解更多信息-您需要清楚地了解您希望在解析方面走多远,以及您愿意使用/您的任务允许使用哪些库。string result=input.Replace(“Console.WriteLine(\”,“”).TrimEnd(“,”,“;”);
Console.WriteLine(\”)yeahh“
-regex很难:)@AlexeiLevenkov你的意思是字符串无法与regex
匹配?我已经测试过,结果是:)但它应该是\”)yeahh
(对不起,我键入了“
而不是上一个;
:控制台。WriteLine(“\”)Yeahh“;
)我尝试过的字符串100%失败。也许你可以添加你尝试过的字符串以及正则表达式的工作方式。@jdpenix为什么+?
而不是+
?而且它不是有效的字符串(带引号)。
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