wtuqi
2/23/2020 - 5:19 AM

正则获取指定规则字符

正则表达式与string.Format搭配
    Public Function Regex(ByVal Value As String, ByVal lenstr As String) As Boolean
        Dim flag As Boolean = False
        Dim patern As String = String.Format("^\d{{{0}}}$", lenstr)'外层的{{ }}只是在正则中表示一组{}
        reg = New Regex(patern)
        flag = reg.IsMatch(Value)
        Return flag
    End Function
'示例:Mstr = (RegexGetStr(Str.Trim, "\d*"))
Public Function RegexGetStr(ByVal oriText As String, ByVal x As String) As String '通过正则获取指定规则字符
    '使用格式GetWord(原文,规则)
   Dim Matches As MatchCollection = Regex.Matches(oriText, x, RegexOptions.IgnoreCase)
   Dim sb As StringBuilder = New StringBuilder()
   For Each NextMatch As Match In Matches
            sb.Append(NextMatch.Value)
            'Debug.Print(NextMatch.Value)
    Next
    Return sb.ToString()
End Function