2

我有一个文件,内容如下:

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
4

3 回答 3

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

演示

于 2013-03-07T13:02:48.133 回答
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
于 2013-03-07T13:35:58.153 回答
1

如果您只想获取“prinfile ps =”之后的值:

printfile ps *= *"([^"]+)";

如果要获取所有键值对:

^([^= ]+) *= *"([^"]+)";$
于 2013-03-07T13:05:54.237 回答