使用regexp在VBS中识别和删除字符串中的字符

时间:2014-10-17 15:48:06

标签: vbscript

我已经半天半决心并研究正则表达式以解决下面的问题而无济于事,所以希望你能帮助我。

我有声音字符串,在部分之间用“//”分隔(也可以在任何地方插入附加的随机“/”)。字符串结尾总是有0到10个斜杠。棘手的部分是删除最后剩余的斜杠“// +”而不删除文本之间的双斜杠“//”。

Example1斜杠结束:

strEmail = "TT44 4 x VCK TTT30 FAX//IT/40170539/CA1211 dd 15.08.12//E1333/City/TS///LC30//75735/01364//---//548657,14-E2424-//34-1/ss//Customer1//LINE1//75739/00096//---//////"

示例2没有斜线结束:

    strEmail = "TT44 4 x VCK TTT30 FAX//IT/40170539/CA1211 dd 15.08.12//E1333/City/TS///LC30//75735/01364//---//548657,14-E2424-//34-1/ss//Customer1//LINE1//75739/00096//---"

斜杠计数可以是动态的,但总是从0到10结束。我认为可以有简单的解决方案,而不需要正则表达式。就像是: 如果在任何斜杠之后没有更多的字母数字字符,请删除字母数字字符后的文本。

谢谢你和问候

2 个答案:

答案 0 :(得分:1)

Option Explicit

Dim strEmail
    strEmail = "TT44 4 x VCK TTT30 FAX//IT/40170539/CA1211 dd 15.08.12//E1333/City/TS///LC30//75735/01364//---//548657,14-E2424-//34-1/ss//Customer1//LINE1//75739/00096//---//////"

    With New RegExp 
        .Pattern = "/+$"
        .IgnoreCase = False
        .Global = True
        strEmail = .Replace(strEmail, "")
    End With
    WScript.Echo strEmail

表示结尾斜杠位于该行的末尾($

已编辑以包含迭代非正则表达式解决方案

Option Explicit

Dim strEmail
    strEmail = "TT44 4 x VCK TTT30 FAX//IT/40170539/CA1211 dd 15.08.12//E1333/City/TS///LC30//75735/01364//---//548657,14-E2424-//34-1/ss//Customer1//LINE1//75739/00096//---//////"

Dim cutPoint
    cutPoint = Len(strEmail)
    Do While cutPoint > 0
        If Not Mid(strEmail,cutPoint,1) = "/" Then Exit Do
        cutPoint = cutPoint - 1
    Loop
    strEmail = Left( strEmail, cutPoint )
    WScript.Echo strEmail
再次

编辑以包含纯VBScript函数替代

Option Explicit

Dim strEmail
    strEmail = "TT44 4 x VCK TTT30 FAX//IT/40170539/CA1211 dd 15.08.12//E1333/City/TS///LC30//75735/01364//---//548657,14-E2424-//34-1/ss//Customer1//LINE1//75739/00096//---//////"

strEmail = Left(strEmail,InStrRev(strEmail, Right(Replace(strEmail,"/",""),1)))
WScript.Echo strEmail

答案 1 :(得分:0)

回家后,我想出了一个简单的解决方案:

Dim slash_count, ch
for i= 1 to len(strEmail)
    ch = mid(strEmail,i,1)
    if ch = "/" Then
        slash_count = slash_count + 1
    Else
        slash_count = 0
    End if
Next
strEmail = left(strEmail,len(strEmail)-slash_count)
msgbox(strEmail)
msgbox(slash_count)