'Namespace: System.Text.RegularExpressions

Regex 數字 檢查: Regex.IsMatch(Me.txtB1.Text, "^[0-9]*[1-9][0-9]*$") 




一些簡單範例:
A. 找出字串是不是Guid
Dim string1 As String = 『2fc3af59-4de5-4c6a-85ac-d9beb64518dc』
Dim instance As New Regex(『\w{8}-\w{4}-\w{4}-\w{4}-\w{12}』)
If instance.IsMatch(string1) Then
Response.Write(『string1 is a Guid』)
Else
Response.Write(『string1 is not a Guid』)
End If
B. 變更日期格式, 以 dd-mm-yy 取代 mm/dd/yy
Function MDYToDMY(input As String) As String
Return Regex.Replace(input, _
『\b(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})\b』, _
『${day}-${month}-${year}』)
End Function
C. 判斷字串是否為正確Email格式
Function IsValidEmail(strIn As String) As Boolean
Return Regex.IsMatch(strIn, _ 『^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$』)
End Function
D.檢查數字及底線
       Dim rx As Regex = New Regex("^[A-Za-z0-9|_|-]+$")
       If rx.IsMatch(Txt1.Text.Trim) Then
          Response.Write("符合標準")
       end if

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 colud77 的頭像
    colud77

    I-Cloud程式攻略

    colud77 發表在 痞客邦 留言(1) 人氣()