簡體   English   中英

VB.Net-使用正則表達式識別雙引號之間的新行

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

我有以下字符串值:

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

基本上,規則是刪除雙引號之間的所有新行。

任何幫助都將受到歡迎!

我想出了下一個解決方案:

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

希望對別人有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM