vbaemailreplaceoutlook

Delete specified text and unspecified number in incoming mail


I use a VBA script to abstract and format data from incoming emails.

I'm trying to devise a way to remove text and a 1 to 5 digit number after the text.

The text is always the word "default" and the number can be from 1 to 99999 (such as "default 23453" or "default 9"). The number could also not exist (just the word "default").

I use:

rmvString = Replace(rmvString, "default ", "")

to remove the word default.

I want to remove the number that usually follows the word default.

Examples:


Input:

Default 2332 additional text comes here.

Output:

additional text comes here.


Another possible input (no number):

Default additional text comes here.

Output:

additional text comes here.


Solution

  • Click Tools, References and add "Microsoft VBScript Regular Expressions 5.5" then you can use a RegExp search & replace:

    rmvString = "Hello default A" & vbCrLf & _
                "Hello DEFAULT 12 B" & vbCrLf & _
                "Hello default 123456 C" & vbCrLf
    
    MsgBox rmvString
    
    With New RegExp
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        .Pattern = "(default\s\d*\s*)" 'finds the string, a space, any digits, any space
    
        rmvString = .Replace(rmvString, "")
    End With
    
    MsgBox rmvString