我有以下字符串,我应该用_
替换每个奇数出现的空格。
字符串:
901 R 902 M 903 Picture_message 904 NA 905 F 906 Local_Relay 907 46
908 51705 909 306910001112/TYPE=PLMN@mms.cosmote.gr
预期字符串:
901_R 902_M 903_Picture_message 904_NA 905_F 906_Local_Relay 907_46
908_51705 909_306910001112/TYPE=PLMN@mms.cosmote.gr
我尝试使用空间计数并使用正则表达式,但无法达到标记。
答案 0 :(得分:4)
循环并保持找到的空格数但仅对奇数进行操作:
Dim thing: thing = "901 R 902 M 903 Picture_message 904 NA 905 F 906 Local_Relay 907 46 908 51705 909 306910001112/TYPE=PLMN@mms.cosmote.gr"
Dim i, counter
For i = 1 To Len(thing)
If Mid(thing, i, 1) = " " Then
counter = counter + 1
If counter Mod 2 Then thing = Left(thing, i - 1) & "_" & Mid(thing, i + 1)
End If
Next
msgbox thing
答案 1 :(得分:2)
一种方法是将字符串拆分为空格,将下划线附加到奇数,将空格附加到偶数元素(最后一个除外),然后将它们粘合在一起:
s = "901 R 902 M 903 Picture_message 904 NA 905 F 906 Loc..."
a = Split(s, " ")
For i = 0 To UBound(a)-1 Step 2
a(i) = a(i) & "_"
Next
For i = 1 To UBound(a)-1 Step 2
a(i) = a(i) & " "
Next
WScript.Echo Join(a, "")
如果你想避免循环两次,你可以一次性完成:
s = "901 R 902 M 903 Picture_message 904 NA 905 F 906 Loc..."
c = CreateObject("ScriptingDictionary")
c.Add 0, "_"
c.Add 1, " "
a = Split(s, " ")
For i = 0 To UBound(a)-1
a(i) = a(i) & c(i Mod 2)
Next
WScript.Echo Join(a, "")
答案 2 :(得分:2)
如果您仍想使用Regular Expression
更新:改进了模式匹配,因此只检测到空格字符,因此如果数据包含902 R
,它仍会返回902_R
。
Dim data: data = "901 R 902 M 903 Picture_message 904 NA 905 F 906 Local_Relay 907 46 908 51705 909 306910001112/TYPE=PLMN@mms.cosmote.gr"
'Include value of first capture group (\b\d{3})
'and append _ to it, this will make up the replacement value.
Dim value: value = "$1_"
Dim result
Dim rx: Set rx = new RegExp
With rx
.Global = True
.IgnoreCase = True
'Expression checks for word boundary followed by 3 digit value
'followed by any number whitespace characters.
.Pattern = "(\b\d{3})\s+"
result = .Replace(data, value)
End With
Set rx = Nothing
WScript.Echo "--------- Test ----------"
WScript.Echo data
WScript.Echo result
WScript.Echo
输出:
--------- Test ----------
901 R 902 M 903 Picture_message 904 NA 905 F 906 Local_Relay 907 46 908 51705 909 306910001112/TYPE=PLMN@mms.cosmote.gr
901_R 902_M 903_Picture_message 904_NA 905_F 906_Local_Relay 907_46 908_51705 909_306910001112/TYPE=PLMN@mms.cosmote.gr
免责声明:我实际上错过了"奇怪"问题标题中的要求,此示例仅使用模式匹配来查找样本数据
<3 digit number><space>
和Replace()
中重复模式的出现次数,其中包含预期的<3 digit number><underscore>
。 < / p>
在旁注上决定根据经典的For
循环方法测试性能,以显示为什么我使用正则表达式切换到此类型的方案,使用@alex-k's example构建一个计时脚本,它还允许我多次复制源数据以创建更大的数据集。
复制源100次
RegEx Method
String Length: 11999
Start: 55250.37109375
Stop: 55250.40234375
Diff: 0.03125
For loop - Mod Method
String Length: 11999
Start: 55250.40234375
Stop: 55250.4375
Diff: 0.03515625
小幅增加1000倍
RegEx Method
String Length: 119999
Start: 55348.5859375
Stop: 55348.9375
Diff: 0.3515625
For loop - Mod Method
String Length: 119999
Start: 55348.9375
Stop: 55350.04296875
Diff: 1.10546875
但是看看如果我们达到5000次
会发生什么RegEx Method
String Length: 599999
Start: 55545.69140625
Stop: 55547.4296875
Diff: 1.73828125
For loop - Mod Method
String Length: 599999
Start: 55547.4296875
Stop: 55584.15234375
Diff: 36.72265625
对For
循环方法的影响是指数级的,当我达到10000次RegEx方法运行时,在尝试运行For
循环方法时无法及时返回