我正在寻找可以帮助我从下面的字符串中提取的正则表达式filename.asp
。这似乎是一个简单的任务,但是我找不到解决方案。
这是我的输入:
<!-- #include file="filename.asp" -->
我想要这样的正则表达式输出:
filename.asp
我进行了一些研究,找到了以下解决方案。
正则表达式:
/#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]*"([^"]+)"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句