Alex Rivera | Logout

vba regex: dot matching newline

Asked 2012-04-24T11:57:12.737
8

I want to match (in this snippet) everything upto but not including a newline which is what I thought a . would do. Could someone shed light on what I'm missing please.

Public Sub regexp()

Dim oRegExp As regexp
Dim oMatches As MatchCollection
Dim oMatch As Match
Dim sString As String

sString = _
    "one" & vbNewLine & _
    "two" & vbNewLine

Set oRegExp = New regexp
With oRegExp
    .Global = True
    .Pattern = ".+"
    Set oMatches = .Execute(sString)
    For Each oMatch In oMatches
        Debug.Print "*" & oMatch.Value & "*"
    Next oMatch
End With

End Sub

Output is

*one
*
*two
*

Expected output

*one*
*two*

How can I avoid the newline in the output? Thanks.

Edit
Report

1 Answer

11

If you use [^\n] in place of ., it will match any character except the new line character.

answered 2012-04-24T12:04:18.413

Your Answer