2015-06-23 55 views
0

我在尋找一個正則表達式可以幫助我從下面的字符串中提取filename.asp。這似乎是一個簡單的任務,但我無法找到解決方案。提取經典ASP的正則表達式包括文件名

這是我輸入:

<!-- #include file="filename.asp" --> 

我想正則表達式的輸出是這樣的:

filename.asp 

回答

1

我做了一些研究,並找到了以下解決方案。

正則表達式:

/#include\W+file="([^"]+)"/g 

實施例的代碼(VB.NET):

Dim list As New List(Of String) 
Dim regex = New System.Text.RegularExpressions.Regex("#include\W+file=""([^""]+)""") 
Dim matchResult = regex.Match(filetext) 
While matchResult.Success 
    list.Add(matchResult.Groups(1).Value) 
    matchResult = matchResult.NextMatch() 
End While 

實施例的代碼(C#):

var list = new List<string>(); 
var regex = new Regex("#include\\W+file=\"([^\"]+)\""); 
var matchResult = regex.Match(fileContent); 
while (matchResult.Success) { 
    list.Add(matchResult.Groups[1].Value); 
    matchResult = matchResult.NextMatch(); 
} 

改進正則表達式(忽略空格):

#include\W+file[\s]*=[\s]*"([^"]+)" 
+0

如果你可以在幾秒鐘內研究,爲什麼你發佈這個問題?! –

+0

還沒有最終答案。上面的代碼在'file ='後面的空格不起作用。 –

+0

它應該工作(即正則表達式),即使在'file =' –