尝试创建一个函数,你有2个过滤器,然后vb经过一长串文本,找到2个过滤器之间的所有文本,并将每个结果存储在一个数组中。
所以如果我在文本文件中有以下内容:
Enter a date in the format of MM/dd/yyyy: 92/23/2344
ERROR: Please input the date in the correct format
Enter a date in the format of MM/dd/yyyy: 11/26/2019
The day of week is Tuesday
由于某种原因,Stackoverflow网页不允许我添加“<”或“>”对于过滤器,它只是清除文本,但您可以了解2个过滤器的含义。
然后我希望vb浏览整个文本页面并返回每个单词并将它们存储在如下数组中:
<Webpage>Hello</Webpage>
SOME_RANDOM_TEXT
<Webpage>There</Webpage>
SOME_RANDOM_TEXT
<Webpage>Testing</Webpage>
SOME_RANDOM_TEXT
<Webpage>Some</Webpage>
SOME_RANDOM_TEXT
<Webpage>Code</Webpage>
Filter 1 = <Webpage>
Filter 2 = </Webpage>
目前我正在做以下操作,它给出了“参数'长度'必须大于或等于零的错误。”但是,如果结果只在整个文本文件中出现一次,则返回OK,似乎没有正在工作,因为有多个结果返回。
Array1(Hello)
Array2(There)
Array3(Testing)
Array4(Some)
Array5(Code)
非常感谢任何帮助。
答案 0 :(得分:0)
如果您逐行阅读文件,请进行处理。
“filepath”是文件在fs上的完整路径。
现在唯一需要考虑的事情是,如果你在同一行中有多个Anchor标签。
Dim pos1 As Integer
Dim pos2 As Integer
Dim Str1 As String
Dim Str2 As String
Dim Line As String
Open "<filepath>" For Input As #1
Do Until EOF(1)
Line Input #1, Line
filter1 = InStr(Line, "<Webpage>")
filter2 = InStr(Line, "</Webpage>")
If filter1 > 0 And filter2 > 0 Then
Str1 = Left(Line, filter2 - 1)
filter3 = InStr(Str1, "<Webpage>")
Str2 = Mid(Str1, filter3 + 9, Len(Str1))
' add Str2 to array
End If
Loop
Close #1
答案 1 :(得分:0)
Dim text As String = "<Webpage>Hello</Webpage>SOME_RANDOM_TEXT<Webpage>There</Webpage>SOME_RANDOM_TEXT<Webpage>Testing</Webpage>SOME_RANDOM_TEXT<Webpage>Some</Webpage>SOME_RANDOM_TEXT<Webpage>Code</Webpage>"
Dim filterStart As String = "<Webpage>"
Dim filterEnd As String = "</Webpage>"
Dim words As New List(Of String)
Do While True
If text.Length = 0 Then
Exit Do
End If
Dim index as Integer = text.IndexOf(filterStart)
If index > -1 Then
text = text.Substring(index + 9)
index = text.IndexOf(filterEnd)
Dim word As String
if index > -1 Then
word = text.Substring(0, index)
words.Add(word)
text = text.Substring(index + 10)
Else
word = text
words.Add(word)
Exit Do
End If
Else
Exit Do
End If
Loop