Search code examples
excelregexvbaoutlook

How to remove line feed?


This script for Outlook returns the desired date but doesn't remove the line feed.

String I want to get the date from:
1_c Anruf/Meldung am (Datum): 04.Mai.2020

With Reg1
    .Pattern = "1_c Anruf\/Meldung am \(Datum\)\s*[:]+\s*(.*)\s+\n"
    .Global = False
End With
If Reg1.Test(olMail.Body) Then
    Set M1 = Reg1.Execute(olMail.Body)
End If
For Each M In M1
    Debug.Print M.SubMatches(0)
    With xExcelApp
        Range("A5").Value = M.SubMatches(0)
    End With
Next M

regex101 selects correctly but the debugger always shows something like "02.12.2020 ". <- containing no whitespace but a line feed.

In Excel the line feed is also visible. Also trailing whitespace isn't a problem since I can use TRIM but the line feed doesn't allow it to function.


Solution

  • Your regex captures the CR symbol, you can replace . with [^\r\n] to avoid this behavior.

    It seems you want to use

    1_c Anruf/Meldung am \(Datum\)\s*:+\s*([^\r\n]*\S)
    

    See the regex demo. Note the forward slash does not have to be escaped in the VBA code. Details:

    • 1_c Anruf/Meldung am \(Datum\) - a fixed 1_c Anruf/Meldung am (Datum) string
    • \s*:+\s* - one or more colons enclosed with zero or more whitespaces
    • ([^\r\n]*\S) - Capturing group 1 (accessed with M.SubMatches(0)) that captures zero or more occurrences of any char other than CR and LF chars and then any non-whitespace char.