为什么 regex.Match 返回空字符串?

Why regex.Match is returning empty string?

我只想获取与正则表达式匹配的字符串部分,但尝试使用 match.Value 或组时总是 returns ""。这让我疯狂。 编辑:

这有效:

Private Function NormalizeValue(ByVal fieldValue As String) As String
    Dim result As String = ""
    Dim pattern As String = "[a-zA-Zñ'-]*"
    Dim matches As Match
    matches = Regex.Match(fieldValue, pattern)
    While (matches.Success = True)
        result = result & matches.Value
        matches = matches.NextMatch()
    End While
    Return result
End Function

如果您的正则表达式以 ^ 开头并以 $ 结尾,则您正在尝试匹配整个字符串 - 而不是您在问题中陈述的部分。 因此,您要么需要删除它们,要么重新表述您的问题。