Trm
Trm

Reputation: 479

Identifying and removing characters from string in VBS using regexp

I've been cracking my head for half day and researching regexp to solve below problem with no avail, so hope you can help me.

I have bellow string that is separated by "//" between parts (can also be additional random "/" inserted anywhere). The string end always has from 0 to 10 slashes. The tricky part is to remove the last remaining slashes "//+" without removing double slash "//" between text.

Example1 slash ending:

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//---//////"

Example2 no slash ending:

    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//---"

Slash count can be dynamic, but will always end from 0 to 10. I think there could be simple solution, without need of regexp. Something like: if after any slash there are no more Alphanumeric characters, remove text after Alphanumeric character.

thank you and regards

Upvotes: 0

Views: 2677

Answers (2)

MC ND
MC ND

Reputation: 70923

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

Indicate that the ending slashes are at the end of the line ($)

edited to include an iterative non regexp solution

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

edited again to include a pure VBScript functions alternative

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

Upvotes: 1

Trm
Trm

Reputation: 479

after having a walk home i came up with simple solution:

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)

Upvotes: 0

Related Questions