脚本文件内容
//{input: x(width),y(height);
//output: z(area);}
function(x,y)
z=x*y
我必须只读这些行。大括号中的数据的正则表达式是什么
//{input: x(width),y(height);
//output: z(area);}
我尝试了以下
Dim sr As StreamReader = New StreamReader(scriptpath)
' Dim textToParse As String
Dim scriptText As String
scriptText = sr.ReadToEnd
Dim extractCommentRegex As New Regex("\/\/\{(.*?)\}")
Dim textToParse As Match = extractCommentRegex.Match(scriptText)
答案 0 :(得分:2)
试试这个
^\/\/\{.*\}
使用/ m选项使点匹配换行符。
嗨,很抱歉没有写vb很长一段时间所以没有给出足够明确的答案。我已经创建了一个控制台项目来测试以下代码:
Dim sr As StreamReader = New StreamReader("d:\script1.txt")
' Dim textToParse As String
Dim scriptText As String
scriptText = sr.ReadToEnd
Dim match = Regex.Match(scriptText, "^\/\/\{.*\}", RegexOptions.Singleline Or RegexOptions.Multiline Or RegexOptions.IgnorePatternWhitespace)
Console.WriteLine(match.Success)
Dim sw As StreamWriter = New StreamWriter("d:\output.txt")
sw.Write(match.Value)
sw.Flush()
sw.Close()
Console.ReadLine()
我会关注output.txt。
//{input: x(width),y(height);
//output: z(area);}
我认为你需要提供RegexOptions如果你有输入文件的Windows格式LF。有关问题的详细信息,请参阅此主题: