本文整理汇总了VB.NET中System.Text.RegularExpressions.Regex.Regex构造函数的典型用法代码示例。如果您正苦于以下问题:VB.NET Regex构造函数的具体用法?VB.NET Regex怎么用?VB.NET Regex使用的例子?那么, 这里精选的构造函数代码示例或许可以为您提供帮助。您也可以进一步了解该构造函数所在类System.Text.RegularExpressions.Regex
的用法示例。
在下文中一共展示了Regex构造函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
' 导入命名空间
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "\b[at]\w+"
Dim text As String = "The threaded application ate up the thread pool as it executed."
Dim matches As MatchCollection
Dim defaultRegex As New Regex(pattern)
' Get matches of pattern in text
matches = defaultRegex.Matches(text)
Console.WriteLine("Parsing '{0}'", text)
' Iterate matches
For ctr As Integer = 0 to matches.Count - 1
Console.WriteLine("{0}. {1}", ctr, matches(ctr).Value)
Next
End Sub
End Module
输出:
Parsing 'The threaded application ate up the thread pool as it executed.' 0. threaded 1. application 2. ate 3. the 4. thread 5. as
示例2: Example
' 导入命名空间
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "\b[at]\w+"
Dim options As RegexOptions = RegexOptions.IgnoreCase Or RegexOptions.Compiled
Dim text As String = "The threaded application ate up the thread pool as it executed."
Dim matches As MatchCollection
Dim optionRegex As New Regex(pattern, options)
Console.WriteLine("Parsing '{0}' with options {1}:", text, options.ToString())
' Get matches of pattern in text
matches = optionRegex.Matches(text)
' Iterate matches
For ctr As Integer = 0 to matches.Count - 1
Console.WriteLine("{0}. {1}", ctr, matches(ctr).Value)
Next
End Sub
End Module
输出:
Parsing 'The threaded application ate up the thread pool as it executed.' with options IgnoreCase, Compiled: 0. The 1. threaded 2. application 3. ate 4. the 5. thread 6. as
示例3: Example
' 导入命名空间
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Security
Imports System.Text.RegularExpressions
Imports System.Threading
Module Example
Const MaxTimeoutInSeconds As Integer = 3
Public Sub Main()
Dim pattern As String = "(a+)+$" ' DO NOT REUSE THIS PATTERN.
Dim rgx As New Regex(pattern, RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1))
Dim sw As Stopwatch = Nothing
Dim inputs() As String = { "aa", "aaaa>",
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"aaaaaaaaaaaaaaaaaaaaaa>",
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>" }
For Each inputValue In inputs
Console.WriteLine("Processing {0}", inputValue)
Dim timedOut As Boolean = False
Do
Try
sw = Stopwatch.StartNew()
' Display the result.
If rgx.IsMatch(inputValue) Then
sw.Stop()
Console.WriteLine("Valid: '{0}' ({1:ss\.fffffff} seconds)",
inputValue, sw.Elapsed)
Else
sw.Stop()
Console.WriteLine("'{0}' is not a valid string. ({1:ss\.fffff} seconds)",
inputValue, sw.Elapsed)
End If
Catch e As RegexMatchTimeoutException
sw.Stop()
' Display the elapsed time until the exception.
Console.WriteLine("Timeout with '{0}' after {1:ss\.fffff}",
inputValue, sw.Elapsed)
Thread.Sleep(1500) ' Pause for 1.5 seconds.
' Increase the timeout interval and retry.
Dim timeout As TimeSpan = e.MatchTimeout.Add(TimeSpan.FromSeconds(1))
If timeout.TotalSeconds > MaxTimeoutInSeconds Then
Console.WriteLine("Maximum timeout interval of {0} seconds exceeded.",
MaxTimeoutInSeconds)
timedOut = False
Else
Console.WriteLine("Changing the timeout interval to {0}",
timeout)
rgx = New Regex(pattern, RegexOptions.IgnoreCase, timeout)
timedOut = True
End If
End Try
Loop While timedOut
Console.WriteLine()
Next
End Sub
End Module
输出:
Processing aa Valid: 'aa' (00.0000779 seconds) Processing aaaa> aaaa>' is not a valid string. (00.00005 seconds) Processing aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Valid: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' (00.0000043 seconds) Processing aaaaaaaaaaaaaaaaaaaaaa> Timeout with 'aaaaaaaaaaaaaaaaaaaaaa>' after 01.00469 Changing the timeout interval to 00:00:02 Timeout with 'aaaaaaaaaaaaaaaaaaaaaa>' after 02.01202 Changing the timeout interval to 00:00:03 Timeout with 'aaaaaaaaaaaaaaaaaaaaaa>' after 03.01043 Maximum timeout interval of 3 seconds exceeded. Processing aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> Timeout with 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>' after 03.01018 Maximum timeout interval of 3 seconds exceeded.