1. 程式人生 > 實用技巧 >VB.NET 中使用正則表示式

VB.NET 中使用正則表示式

fderStr2 = System.Text.RegularExpressions.Regex.Replace(fderStr1, "^([^s]*)\(", "")

其實只要一句就可以了,表示式為替換( 括號之前的所有東西
fderStr2 = System.Text.RegularExpressions.Regex.Replace(fderStr1, "^([^s]*)\(", "")

關於正則表示式的使用
Dim xRegex As New System.Text.RegularExpressions.Regex(1)
括號裡的1 改成 0 也一樣的,目前不知道是什麼作用,但必須得填
fderStr2 = xRegex.Replace(fderStr1, "^([^s]*)\(", "")

替換三個引數,
Dim xRegex As New System.Text.RegularExpressions.Regex(1)
fderStr2 = xRegex.Replace(fderStr1, "^([^s]*)\(", "")
會報警告,改成下面的,不會報
fderStr2 = System.Text.RegularExpressions.Regex.Replace(fderStr1, "^([^s]*)\(", "")

        If Label5.Text <> "Label5" And Label6.Text <> "" And Label5.Text <> ""
Then Dim xRegex As New System.Text.RegularExpressions.Regex(1) Dim fder1Path, fder2Path, fder3Path, fderStr1, fderStr2, fderStr3, fderStr4 As String fder1Path = Trim(Label5.Text) fder2Path = Trim(Label6.Text) fder3Path = Trim(Label7.Text)
Dim dir As New System.IO.DirectoryInfo(fder1Path) For Each d As System.IO.DirectoryInfo In dir.GetDirectories ' Dim finfo As New System.IO.FileInfo(d.Name) 'If (finfo.Attributes And System.IO.FileAttributes.Hidden) <> System.IO.FileAttributes.Hidden Then 'End If fderStr1 = d.Name ' fderStr2 = System.Text.RegularExpressions.Regex("^([^s]*)\(", "") fderStr2 = xRegex.Replace(fderStr1, "^([^s]*)\(", "") MsgBox(fderStr2) Next Else MsgBox("請 選擇 三個資料夾", MsgBoxStyle.Exclamation, "提示") End If