簡體   English   中英

用“_”替換每個奇數出現的空格

[英]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.

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