[英]Replace every odd occurrence of space with “_”
我有以下字符串,我应该用_
替换每个奇数出现的空格。
串:
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
我尝试使用空间计数并使用正则表达式,但无法达到标记。
循环并保持找到的空格数但仅对奇数进行操作:
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
一种方法是在空格处拆分字符串,将下划线附加到奇数和空格到偶数元素(最后一个除外),然后将它们粘合在一起:
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, "")
如果您仍想使用正则表达式
更新:改进了模式匹配,因此只检测到空白字符,因此如果数据包含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>
。
在侧面说明决定测试性能与经典的For
循环方法,以显示为什么我切换到使用正则表达式这种类型的场景,使用@ alex-k的 示例构建了一个时序脚本,也允许我复制源数据多次创建更大的数据集。
复制源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
循环方法时无法及时返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.