VB.Net - 使用正则表达式识别双引号之间的新行

时间:2013-10-30 16:07:13

标签: .net regex vb.net

我有以下字符串值:

1A-2A-3A-"Comment line 1
Comment line 2
Comment line A1
Comment line A2"-BREAK1
1B-2B-3B-"Comment line 3
Comment line 4"-BREAK2
1C-2C-3C-4C-BREAK3
1D-2D-3D-4D-BREAK4

我想在vb.net中使用正则表达式,它给出了以下结果:

1A-2A-3A-"Comment line 1|Comment line 2|Comment line A1|Comment line A2"-BREAK1
1B-2B-3B-"Comment line 3|Comment line 4"-BREAK2
1C-2C-3C-4C-BREAK3
1D-2D-3D-4D-BREAK4

基本上,规则是删除双引号之间的任何新行。

欢迎任何帮助!

1 个答案:

答案 0 :(得分:0)

我找到了下一个解决方案:

Dim vInput = <xml>1A-2A-3A-"Comment line 1
Comment line 2
Comment line A1
Comment line A2"-BREAK1
1B-2B-3B-"Comment line 3
Comment line 4"-BREAK2
1C-2C-3C-4C-BREAK3
1D-2D-3D-4D-BREAK4</xml>.Value

Dim vRegExMatch = System.Text.RegularExpressions.Regex.Matches(vInput, """[^""\\]*(?:\\.[^""\\]*)*""|'[^'\\]*(?:\\.[^'\\]*)*'")
Dim vSpecialChar As Char = "|" '"†"

For Each vMatch In vRegExMatch
    vInput = vInput.Replace(CStr(vMatch.Value), CStr(vMatch.Value).Replace(vbLf, vSpecialChar))
Next

结果:

1A-2A-3A-"Comment line 1|Comment line 2|Comment line A1|Comment line A2"-BREAK1
1B-2B-3B-"Comment line 3|Comment line 4"-BREAK2
1C-2C-3C-4C-BREAK3
1D-2D-3D-4D-BREAK4

希望对某人有所帮助!