vba 正则表达式:点匹配换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10297365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:55:02  来源:igfitidea点击:

vba regex: dot matching newline

regexvba

提问by wfsp

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.

如何避免输出中的换行符?谢谢。

回答by eggyal

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

如果使用[^\n]代替.,它将匹配换行符以外的任何字符。