Vb.net 在文本文件中提取行的一部分
我试图从pdf中提取一些特定的数据,我已经设法从pdf中提取文本并将其放入txt文件中。放置在文本文件中的数据是一条长线。我需要提取线路的一个特定部分 如果以“UK”开头,以“-”结尾 我一直在尝试使用Vb.net 在文本文件中提取行的一部分,vb.net,Vb.net,我试图从pdf中提取一些特定的数据,我已经设法从pdf中提取文本并将其放入txt文件中。放置在文本文件中的数据是一条长线。我需要提取线路的一个特定部分 如果以“UK”开头,以“-”结尾 我一直在尝试使用 Using read = New StreamReader(fName) Dim line As String = read.ReadToEnd If line.StartsWith(" UK") And line.Contains("- -")
Using read = New StreamReader(fName)
Dim line As String = read.ReadToEnd
If line.StartsWith(" UK") And line.Contains("- -") Then
Else
'do nothing
End If
End Using
Startswith
不起作用,因为该行不是以“UK”开头的。我可以使用line.contains
查找UK,但该行包含多个'-'实例
我需要的部分如下所示
英国(0.6085)*(英镑)1.6435-0.00620.8206-0.0017--
我正在MS Visual studio 2013中使用vb.net
有人能提供一些帮助吗?尝试使用Regex类:
Dim regex As New Regex("UK.*-\s?-\s?", RegexOptions.Singleline)
Dim match As Match = regex.Match(a)
If match.Success Then
' Do stuff
End If
在If..中,您可以通过Match.Captures集合属性循环一系列匹配
For Each c As Capture In result.Captures
' c.Value
Next
正则表达式是文本匹配、提取等方面的一个很好的工具。如果你做了相当多的工作,就要习惯使用正则表达式。在将.NET正则表达式模式用于代码之前,我发现在动态测试它们时非常方便。StartWith和EndsWith如何
if (src.StartsWith("UK") AND src.EndsWith("- -")) Then
'True
End If
简单解决方案:
If line like "*UK*- -" then
'do something
Else
'do nothing
End If
您实际需要提取字符串的哪一部分?字符串有数百个字符,上面突出显示了我想要的部分。好的,我想您可能实际需要数字或特定部分。