我有这样的文字。
(57) some text
some other text
unknown nubmer of such lines
(12) some text
我想将其缩小为
(57) some text some other text unknown nubmer of such lines
(12) some text
表示空格分隔而不是换行符。
是否可以使用正则表达式查找和替换,或者我需要编写脚本?
我能够找到使用:\(\d{1,2}\)([\w\W]*?)\(\d{1,2}\)
事实证明,我的数据不一致,需要定义开始和结束。
答案 0 :(得分:4)
您似乎需要基于正则表达式的替换。使用
[\r\n]+(?!\(\d+\))
并替换为空格。此表达式将匹配未跟随(
+数字+ )
的所有换行符。
请参阅regex demo
Dim regEx As Object
Dim result As String
Set regEx = CreateObject("VBScript.RegExp")
With regEx
.pattern = "[\r\n]+(?!\(\d+\))"
.Global = True
.MultiLine = False
End With
s = "(57) some text" & vbCrLf & "some other text" & vbCrLf & "unknown nubmer of such lines" & vbCrLf & "(12) some text" & vbCrLf & "(54) sometext" & vbCrLf & "some Text" & vbCrLf & "unknown nubmer of such lines" & vbCrLf & "(51) some text" & vbCrLf & "(86) some text" & vbCrLf & "some Text" & vbCrLf & "some other text" & vbCrLf & "(87) some text"
result = regEx.Replace(s, " ")
结果:
(57) some text some other text unknown nubmer of such lines
(12) some text
(54) sometext some Text unknown nubmer of such lines
(51) some text
(86) some text some Text some other text
(87) some text