我有一个包含一些行的文件。我设法找到了循环中的所有行。我需要的代码行如下所示:(PS:我认为可以使用正则表达式来实现这一点。)
if "%testfile%"=="abcd" (此文件包含更多这样的行,但abcd更改为任何内容。也可以有不同的空格,比如
if "%testfile%" =="abcd" ( 等。
我想要得到
abcd在我的循环内的一个变量中,我可以进一步使用它。
下面的部分就是这样做的:( lineArray.Item(x)包含整行)
For x = 0 To lineArray.Count - 1
If lineArray.Item(x).Contains("%testfile%") Then
MsgBox(lineArray.Item(x)) 'here should it be done.
End If
Next发布于 2018-06-12 00:57:15
您完全正确,这可以使用正则表达式来完成。我建议使用以下模式:
(?<=if\s+"%testfile%"\s*==\s*)".*?"(?=\s+\()Online Demo
说明
(?<=if\s+"%testfile%"\s*==\s*) 用额外的 + / 可选 * 空格 \s 向后断言有问题的行“。*?” layz 匹配目标字符串(?=\s+\() 前视作为后锚Imports System
Imports System.Text.RegularExpressions
Public Class Example
Public Shared Sub Main()
Dim pattern As String = "(?<=if\s+""%testfile%""\s*==\s*)"".*""(?=\s+\()"
Dim input As String = "if ""%testfile%"" ==""abcd"" ( "
Dim options As RegexOptions = RegexOptions.Multiline
For Each m As Match In Regex.Matches(input, pattern, options)
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index)
Next
End Sub
End Classhttps://stackoverflow.com/questions/50797577
复制相似问题