2010年6月28日 星期一

好用的 regex

這兒看到這篇 regex 的運用
依原例,我把它強化了;底下親自體驗一下這regex 帶來的方便性:
regex:表示不變的規則運算式


程式碼:
Imports System.Text.RegularExpressions

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim reg As New Regex("\d+")
Dim m As Match
Dim TmpStr As String = Label1.Text
TextBox1.Text = ""

For i As Integer = 0 To Label1.Text.Length
m = reg.Match(Label1.Text)
If m.Success Then
TextBox1.Text += m.Value
Label1.Text = Label1.Text.Replace(m.Value, "")
Else
Exit For
End If
Next
Label1.Text = TmpStr
End Sub
End Class

-------------
以上有看到嗎? 若採用一般的作法:迴圈+ascii , 這整體效率就差了

沒有留言:

張貼留言