描述
我认为简单地使用正则表达式匹配例程会更容易,然后输出每一行。这个正则表达式将:
- 假设
\r\n
等效于 VbCrLf
- 匹配整行和文本
- 修剪分隔符
\r\n
以模仿 split 命令如何删除分隔符
- 即使引用的字符串包含一个
\r\n
^(?:[^"\r\n]|"[^"]*")*(?=\r\n|\Z)
例子
正则表达式的现场演示
示例文本
line 1
line 2
line 3 "this is
line 3a
line 3b
line 3c" and some more text
line 4
line 5
代码
VB.NET Code Example:
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim sourcestring as String = "replace with your source string"
Dim re As Regex = New Regex("^(?:[^""\r\n]|""[^""]*"")*(?=\r\n|\Z)",RegexOptions.IgnoreCase OR RegexOptions.IgnorePatternWhitespace OR RegexOptions.Multiline OR RegexOptions.Singleline)
Dim mc as MatchCollection = re.Matches(sourcestring)
Dim mIdx as Integer = 0
For each m as Match in mc
For groupIdx As Integer = 0 To m.Groups.Count - 1
Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames(groupIdx), m.Groups(groupIdx).Value)
Next
mIdx=mIdx+1
Next
End Sub
End Module
找到的匹配项
[0][0] = line 1
[1][0] = line 2
[2][0] = line 3 "this is
line 3a
line 3b
line 3c" and some more text
[3][0] = line 4
[4][0] = line 5