Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vba regex: dot matching newline

Tags:

regex

vba

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.

like image 933
wfsp Avatar asked Apr 24 '12 11:04

wfsp


1 Answers

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

like image 98
eggyal Avatar answered Oct 01 '22 11:10

eggyal