我有一个文件,内容如下:
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
最佳答案
你真的需要一个正则表达式吗? 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
关于regex - 捕获特定子字符串的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15271714/