0

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

2 Answers 2

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

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
Sign up to request clarification or add additional context in comments.

2 Comments

hi. thanks, i see the logic, but it does not really replace slashes when i run it. I also posted another solution avoiding regexp, but I mark you as answer for regexp.
@Trm, , well, just to be precise, it was replacing the slashes. What it didn't do is to store it again in the variable. The value was only echoed. Anyway, i've changed the answer to save the changes and included a iterative solution (as the "problem" is at the end of the string it is easier/faster to start at the end and then go backwards) and a direct vbscript functions alternative. .
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.