问题描述:

script file content is

//{input: x(width),y(height);

//output: z(area);}

function(x,y)

z=x*y

I have to read only these lines. What will be the regular expression for data that is in curly braces

//{input: x(width),y(height);

//output: z(area);}

I tried the following

 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)

网友答案:

Try this

^\/\/\{.*\}

with /m option to make dot match newlines.

Hi, sorry haven't been writing vb for quite a while so did not make the answer clear enough. I've created a console project to test following code:

    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()

And I'll get following for output.txt.

//{input: x(width),y(height);
//output: z(area);}

I think you need to provide RegexOptions If you have Windows format LF for the input file. for detail of the issue please see this thread:

.NET Regex dot character matches carriage return?

相关阅读:
Top