本文整理匯總了VB.NET中System.Text.RegularExpressions.Regex.Replace方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Regex.Replace方法的具體用法?VB.NET Regex.Replace怎麽用?VB.NET Regex.Replace使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Text.RegularExpressions.Regex
的用法示例。
在下文中一共展示了Regex.Replace方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
' 導入命名空間
Imports System.Collections
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim words As String = "letter alphabetical missing lack release " + _
"penchant slack acryllic laundry cease"
Dim pattern As String = "\w+ # Matches all the characters in a word."
Dim evaluator As MatchEvaluator = AddressOf WordScrambler
Console.WriteLine("Original words:")
Console.WriteLine(words)
Try
Console.WriteLine("Scrambled words:")
Console.WriteLine(Regex.Replace(words, pattern, evaluator,
RegexOptions.IgnorePatternWhitespace,
TimeSpan.FromSeconds(.25)))
Catch e As RegexMatchTimeoutException
Console.WriteLine("Word Scramble operation timed out.")
Console.WriteLine("Returned words:")
End Try
End Sub
Public Function WordScrambler(match As Match) As String
Dim arraySize As Integer = match.Value.Length - 1
' Define two arrays equal to the number of letters in the match.
Dim keys(arraySize) As Double
Dim letters(arraySize) As Char
' Instantiate random number generator'
Dim rnd As New Random()
For ctr As Integer = 0 To match.Value.Length - 1
' Populate the array of keys with random numbers.
keys(ctr) = rnd.NextDouble()
' Assign letter to array of letters.
letters(ctr) = match.Value.Chars(ctr)
Next
Array.Sort(keys, letters, 0, arraySize, Comparer.Default)
Return New String(letters)
End Function
End Module
輸出:
Original words: letter alphabetical missing lack release penchant slack acryllic laundry cease Scrambled words: etlert liahepalbcat imsgsni alkc ereelsa epcnnaht lscak cayirllc alnyurd ecsae
示例2: Example
' 導入命名空間
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
' Get drives available on local computer and form into a single character expression.
Dim drives() As String = Environment.GetLogicalDrives()
Dim driveNames As String = Nothing
For Each drive As String In drives
driveNames += drive.Substring(0,1)
Next
' Create regular expression pattern dynamically based on local machine information.
Dim pattern As String = "\\\\" + Environment.MachineName + "(?:\.\w+)*\\([" + driveNames + "])\$"
Dim replacement As String = "$1:"
Dim uncPaths() AS String = {"\\MyMachine.domain1.mycompany.com\C$\ThingsToDo.txt", _
"\\MyMachine\c$\ThingsToDo.txt", _
"\\MyMachine\d$\documents\mydocument.docx" }
For Each uncPath As String In uncPaths
Console.WriteLine("Input string: " + uncPath)
Dim localPath As String = Nothing
Try
localPath = Regex.Replace(uncPath, pattern, replacement,
RegexOptions.IgnoreCase,
TimeSpan.FromSeconds(0.5))
Console.WriteLine("Returned string: " + localPath)
Catch e As RegexMatchTimeoutException
Console.WriteLine("The replace operation timed out.")
Console.WriteLine("Returned string: " + localPath)
If uncPath.Equals(localPath) Then
Console.WriteLine("Equal to original path.")
Else
Console.WriteLine("Original string: " + uncPath)
End If
End Try
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output if run on a machine whose name is
' MyMachine:
' Input string: \\MyMachine.domain1.mycompany.com\C$\ThingsToTo.txt
' Returned string: C:\ThingsToDo.txt
'
' Input string: \\MyMachine\c$\ThingsToDo.txt
' Returned string: c:\ThingsToDo.txt
'
' Input string: \\MyMachine\d$\documents\mydocument.docx
' Returned string: d:\documents\mydocument.docx
示例3: Example
' 導入命名空間
Imports System.Collections
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim words As String = "letter alphabetical missing lack release " + _
"penchant slack acryllic laundry cease"
Dim pattern As String = "\w+ # Matches all the characters in a word."
Dim evaluator As MatchEvaluator = AddressOf WordScrambler
Console.WriteLine("Original words:")
Console.WriteLine(words)
Console.WriteLine("Scrambled words:")
Console.WriteLine(Regex.Replace(words, pattern, evaluator,
RegexOptions.IgnorePatternWhitespace))
End Sub
Public Function WordScrambler(match As Match) As String
Dim arraySize As Integer = match.Value.Length - 1
' Define two arrays equal to the number of letters in the match.
Dim keys(arraySize) As Double
Dim letters(arraySize) As Char
' Instantiate random number generator'
Dim rnd As New Random()
For ctr As Integer = 0 To match.Value.Length - 1
' Populate the array of keys with random numbers.
keys(ctr) = rnd.NextDouble()
' Assign letter to array of letters.
letters(ctr) = match.Value.Chars(ctr)
Next
Array.Sort(keys, letters, 0, arraySize, Comparer.Default)
Return New String(letters)
End Function
End Module
輸出:
Original words: letter alphabetical missing lack release penchant slack acryllic laundry cease Scrambled words: etlert liahepalbcat imsgsni alkc ereelsa epcnnaht lscak cayirllc alnyurd ecsae
示例4: Example
' 導入命名空間
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim input As String = "Instantiating a New Type" + vbCrLf + _
"Generally, there are two ways that an" + vbCrLf + _
"instance of a class or structure can" + vbCrLf + _
"be instantiated. "
Dim pattern As String = "^.*$"
Dim replacement As String = vbCrLf + "$&"
Dim rgx As New Regex(pattern, RegexOptions.Multiline)
Dim result As String = String.Empty
Dim match As Match = rgx.Match(input)
' Double space all but the first line.
If match.Success Then
result = rgx.Replace(input, replacement, -1, match.Index + match.Length + 1)
End If
Console.WriteLine(result)
End Sub
End Module
輸出:
Instantiating a New Type Generally, there are two ways that an instance of a class or structure can be instntiated.
示例5: Example
' 導入命名空間
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
' Get drives available on local computer and form into a single character expression.
Dim drives() As String = Environment.GetLogicalDrives()
Dim driveNames As String = Nothing
For Each drive As String In drives
driveNames += drive.Substring(0,1)
Next
' Create regular expression pattern dynamically based on local machine information.
Dim pattern As String = "\\\\" + Environment.MachineName + "(?:\.\w+)*\\([" + driveNames + "])\$"
Dim replacement As String = "$1:"
Dim uncPaths() AS String = {"\\MyMachine.domain1.mycompany.com\C$\ThingsToDo.txt", _
"\\MyMachine\c$\ThingsToDo.txt", _
"\\MyMachine\d$\documents\mydocument.docx" }
For Each uncPath As String In uncPaths
Console.WriteLine("Input string: " + uncPath)
Console.WriteLine("Returned string: " + Regex.Replace(uncPath, pattern, replacement, RegexOptions.IgnoreCase))
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output if run on a machine whose name is
' MyMachine:
' Input string: \\MyMachine.domain1.mycompany.com\C$\ThingsToTo.txt
' Returned string: C:\ThingsToDo.txt
'
' Input string: \\MyMachine\c$\ThingsToDo.txt
' Returned string: c:\ThingsToDo.txt
'
' Input string: \\MyMachine\d$\documents\mydocument.docx
' Returned string: d:\documents\mydocument.docx
示例6: Example
' 導入命名空間
Imports System.Collections
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim words As String = "letter alphabetical missing lack release " + _
"penchant slack acryllic laundry cease"
Dim pattern As String = "\w+"
Dim evaluator As MatchEvaluator = AddressOf WordScrambler
Console.WriteLine("Original words:")
Console.WriteLine(words)
Console.WriteLine("Scrambled words:")
Console.WriteLine(Regex.Replace(words, pattern, evaluator))
End Sub
Public Function WordScrambler(match As Match) As String
Dim arraySize As Integer = match.Value.Length - 1
' Define two arrays equal to the number of letters in the match.
Dim keys(arraySize) As Double
Dim letters(arraySize) As Char
' Instantiate random number generator'
Dim rnd As New Random()
For ctr As Integer = 0 To match.Value.Length - 1
' Populate the array of keys with random numbers.
keys(ctr) = rnd.NextDouble()
' Assign letter to array of letters.
letters(ctr) = match.Value.Chars(ctr)
Next
Array.Sort(keys, letters, 0, arraySize, Comparer.Default)
Return New String(letters)
End Function
End Module
輸出:
Original words: letter alphabetical missing lack release penchant slack acryllic laundry cease Scrambled words: elrtte iaeabatlpchl igmnssi lcka aerslee hnpatnce ksacl lialcryc dylruna ecase
示例7: Example
' 導入命名空間
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim input As String = "This is text with far too much " + _
"white space."
Dim pattern As String = "\s+"
Dim replacement As String = " "
Dim result As String = Regex.Replace(input, pattern, replacement)
Console.WriteLine("Original String: {0}", input)
Console.WriteLine("Replacement String: {0}", result)
End Sub
End Module
輸出:
Original String: This is text with far too much white space. Replacement String: This is text with far too much white space.
示例8: Example
' 導入命名空間
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
' Get drives available on local computer and form into a single character expression.
Dim drives() As String = Environment.GetLogicalDrives()
Dim driveNames As String = Nothing
For Each drive As String In drives
driveNames += drive.Substring(0,1)
Next
' Create regular expression pattern dynamically based on local machine information.
Dim pattern As String = "\\\\(?i:" + Environment.MachineName + ")(?:\.\w+)*\\((?i:[" + driveNames + "]))\$"
Dim replacement As String = "$1:"
Dim uncPaths() AS String = {"\\MyMachine.domain1.mycompany.com\C$\ThingsToDo.txt", _
"\\MyMachine\c$\ThingsToDo.txt", _
"\\MyMachine\d$\documents\mydocument.docx" }
For Each uncPath As String In uncPaths
Console.WriteLine("Input string: " + uncPath)
Console.WriteLine("Returned string: " + Regex.Replace(uncPath, pattern, replacement))
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output if run on a machine whose name is
' MyMachine:
' Input string: \\MyMachine.domain1.mycompany.com\C$\ThingsToTo.txt
' Returned string: C:\ThingsToDo.txt
'
' Input string: \\MyMachine\c$\ThingsToDo.txt
' Returned string: c:\ThingsToDo.txt
'
' Input string: \\MyMachine\d$\documents\mydocument.docx
' Returned string: d:\documents\mydocument.docx
示例9: Example
' 導入命名空間
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim str As String = "aabccdeefgghiijkklmm"
Dim pattern As String = "(\w)\1"
Dim replacement As String = "$1"
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(str, replacement, 5)
Console.WriteLine("Original String: '{0}'", str)
Console.WriteLine("Replacement String: '{0}'", result)
End Sub
End Module
輸出:
Original String: 'aabccdeefgghiijkklmm' Replacement String: 'abcdefghijkklmm'
示例10: RegExSample
' 導入命名空間
Imports System.Text.RegularExpressions
Module RegExSample
Function CapText(ByVal m As Match) As String
' Get the matched string.
Dim x As String = m.ToString()
' If the first char is lower case...
If Char.IsLower(x.Chars(0)) Then
' Capitalize it.
Return Char.ToUpper(x.Chars(0)) & x.Substring(1, x.Length - 1)
End If
Return x
End Function
Sub Main()
Dim text As String = "four score and seven years ago"
Console.WriteLine($"text=[{text}]")
Dim rx As New Regex("\w+")
Dim result As String = rx.Replace(text, AddressOf RegExSample.CapText)
Console.WriteLine($"result=[{result}]")
End Sub
End Module
輸出:
text=[four score and seven years ago] result=[Four Score And Seven Years Ago]
示例11: Example
' 導入命名空間
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim input As String = "This is text with far too much " + _
"white space."
Dim pattern As String = "\s+"
Dim replacement As String = " "
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, replacement)
Console.WriteLine("Original String: {0}", input)
Console.WriteLine("Replacement String: {0}", result)
End Sub
End Module
輸出:
Original String: This is text with far too much white space. Replacement String: This is text with far too much white space.
示例12: Example
' 導入命名空間
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "(\p{Sc}\s?)?(\d+\.?((?<=\.)\d+)?)(?(1)|\s?\p{Sc})?"
Dim input As String = "$17.43 €2 16.33 £0.98 0.43 £43 12€ 17"
Dim replacement As String = "$2"
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, replacement)
Console.WriteLine("Original String: '{0}'", input)
Console.WriteLine("Replacement String: '{0}'", result)
End Sub
End Module
輸出:
Original String: '$17.43 €2 16.33 £0.98 0.43 £43 12€ 17' Replacement String: '17.43 2 16.33 0.98 0.43 43 12 17'
示例13: Example
' 導入命名空間
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim input As String = "deceive relieve achieve belief fierce receive"
Dim pattern As String = "\w*(ie|ei)\w*"
Dim rgx As New Regex(pattern, RegexOptions.IgnoreCase)
Console.WriteLine("Original string: " + input)
Dim result As String = rgx.Replace(input, AddressOf ReverseLetter,
input.Split(" "c).Length \ 2)
Console.WriteLine("Returned string: " + result)
End Sub
Public Function ReverseLetter(match As Match) As String
Return Regex.Replace(match.Value, "([ie])([ie])", "$2$1",
RegexOptions.IgnoreCase)
End Function
End Module
輸出:
Original string: deceive relieve achieve belief fierce receive Returned string: decieve releive acheive belief fierce receive