[英]Get only part of a text file
I am building a VB Parser and I have to parse VB classes as file. 我正在构建一个VB解析器,我必须将VB类解析为文件。
Right now I'm proceeding like this: 现在,我正在这样进行:
Dim lines As List(Of String) = File.ReadAllLines(fileName).ToList()
Where fileName
is the actual path of the vb class file to parse. 其中,
fileName
是要解析的vb类文件的实际路径。 However my main concern is that it actually get ALL the lines in the files. 但是,我主要关心的是它实际上获取了文件中的所有行。
So say that I have a vb.class that looks like this: 所以说我有一个vb.class像这样:
Public Class Class1VB
Friend item1 As String
Friend item2 As String
Friend item3 As String
Friend item4 As String
Friend item5 As String
Friend item6 As String
Public Sub New()
MyBase.new()
End Sub
Public Overrides Sub InstanciateVariables()
item1 = New String()
item2 = New String()
item3 = New String()
item4 = New String()
item5 = New String()
item6 = New String()
End Sub
End Class
#End Region
And I only want the item located in the InstanciateVariables()
Sub in the lines
array, is there a way to do that using Linq
or any VB logic available? 而且我只想要位于
lines
数组中InstanciateVariables()
子项中的项,有没有办法使用Linq
或任何可用的VB逻辑来做到这一点?
How long are your files? 您的文件多长时间? If they are not huge, you could load the contents of the entire file into a single string and then use String.IndexOf() to identify the location within the string where the "InstanciateVariables()" code appears.
如果它们不是很大,则可以将整个文件的内容加载到单个字符串中,然后使用String.IndexOf()标识字符串中“ InstanciateVariables()”代码出现的位置。 Next,locate the subsequent location where the "End sub" appears and then perform A string.substring() operation to pull out the desired contents.
接下来,找到“ End sub”出现的后续位置,然后执行string.substring()操作以提取所需的内容。
您可以1)将整个列表放入ListOfAllLines,2)循环,逐行阅读每一行,直到找到显示“ InstanceVariables”的行,3)循环,从ListOfAllLines复制到ListOfImportantLines,直到“ End Sub”,4)停止读取ListOfAllLines并将其删除。
If you use File.ReadLines()
, instead of File.ReadAllLines()
, you get an IEnumerable(Of String)
which you can use Linq to query, while reading the minimum number of lines required. 如果使用
File.ReadLines()
而不是File.ReadAllLines()
,则会得到一个IEnumerable(Of String)
,可以使用Linq进行查询,同时读取所需的最小行数。 eg This would get the contents of InstanciateVariables
, without the Sub
and End Sub
lines, and would only read up to the End Sub
line. 例如,这将获取
InstanciateVariables
的内容 ,而没有Sub
和End Sub
行,并且只会读取End Sub
行。
Dim lines = File.ReadLines(fileName)
Dim selectedLines = lines.
SkipWhile(Function(x) Not x.Contains("InstanciateVariables()")).
Skip(1).
TakeWhile(Function(x) Not x.Contains("End Sub"))
For Each l In selectedLines
Console.WriteLine(l)
Next
It still has to read all the lines before the Public Overrides Sub InstanciateVariables()
line - I don't know of any way to avoid that. 它仍然必须阅读
Public Overrides Sub InstanciateVariables()
行之前的所有行-我不知道有什么方法可以避免这种情况。
If you want the Sub
and End Sub
lines it's a little more tricky, but you could create an extension method to take lines inclusively: 如果您想要
Sub
和End Sub
行比较麻烦,但是可以创建一个扩展方法来包含行:
<Extension>
Public Iterator Function TakeUntilInclusive(Of T)(data As IEnumerable(Of T), predicate As Func(Of T, Boolean)) As IEnumerable(Of T)
For Each item In data
Yield item
If predicate(item) Then
Return
End If
Next
End Function
And then use that: 然后使用:
Dim lines = File.ReadLines(fileName)
Dim selectedLines = lines.
SkipWhile(Function(x) Not x.Contains("InstanciateVariables()")).
TakeUntilInclusive(Function(x) x.Contains("End Sub"))
For Each l In selectedLines
Console.WriteLine(l)
Next
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.