我的文件内容如下:
garbage
garbage
printfile ps = "Elopak Zufriedenheit After Sales.ps";
garbage
garbage
我想捕捉""
(双引号)之间的部分 - 但该部分的值不断变化。我写了正则表达式来查找printfile ps
- 但是从那里开始的最佳方式是什么?
新代码:
Dim PFileRegex As New Regex("printfile ps *\t*= *\t*""[\w\s]*.ps\s*""")
Dim PFilematch As Match = PFileRegex.Match(parttext)
If PFilematch.Success = True Then
Dim Quote As Char = """"c
Dim FirstQuote = PFilematch.Value.IndexOf(Quote)
Dim LastQuote = PFilematch.Value.LastIndexOf(Quote)
Dim PSFile = PFilematch.Value.Substring(FirstQuote + 1, (LastQuote - FirstQuote) - 1)
Debug.Print(PSFile)
Else
'error handlung
End If
答案 0 :(得分:2)
你真的需要一个正则表达式吗? String.Substring
效率更高。
Dim quote As Char = """"c
Dim first = printfilePs.IndexOf(quote)
Dim last = printfilePs.LastIndexOf(quote)
Dim partBetweenQuotes = ""
If first <> -1 AndAlso last > first Then
Dim start = first + 1
Dim length = printfilePs.Length - start - (printfilePs.Length - last)
partBetweenQuotes = printfilePs.Substring(start, length)
End If
答案 1 :(得分:1)
如果您只想获取“prinfile ps =”之后的值:
printfile ps *= *"([^"]+)";
如果你想获得所有关键值夫妻:
^([^= ]+) *= *"([^"]+)";$
答案 2 :(得分:1)
以下是使用Regex
类:
Dim PFileRegex As New Regex("^printfile ps\s*=\s*""(?<data>.*\.ps\s*)"";.*$", RegexOptions.Multiline)
Dim PFilematch As Match = PFileRegex.Match(parttext)
If PFilematch.Success Then
Debug.Print(PFilematch.Result("${data}"))
End If