2013-10-22 52 views
0

腳本文件的內容就是Regex-匹配每一個在支架包括換行符

//{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) 

回答

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。有關問題的詳細信息,請參閱此主題:

.NET Regex dot character matches carriage return?

+0

其不工作 – user2194838

相關問題