简体   繁体   English

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

[英]VB.Net - Use regular expression to identify new lines between double quotes

I have the following string value: 我有以下字符串值:

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

I want use a regular expression in vb.net that give me the following result: 我想在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

Basically the rule is to remove any new line between double quotes. 基本上,规则是删除双引号之间的所有新行。

Any help will be welcome! 任何帮助都将受到欢迎!

I figured out the next solution: 我想出了下一个解决方案:

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

Result: 结果:

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

Hope that helps to somebody! 希望对别人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM