在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
正则表达式是可以与输入文本匹配的模式。 .Net框架提供了允许这种匹配的正则表达式引擎。 模式由一个或多个字符文字,运算符或构造组成。 定义正则表达式的构造有各种类别的字符,运算符和构造,允许您定义正则表达式。 单击以下链接以查找这些结构。 正则表达式类正则表达式类用于表示一个正则表达式。 正则表达式类有以下常用方法:
有关方法和属性的完整列表,请参阅Microsoft文档。 示例1以下示例匹配以“S”开头的单词: Imports System.Text.RegularExpressions Module regexProg Sub showMatch(ByVal text As String, ByVal expr As String) Console.WriteLine("The Expression: " + expr) Dim mc As MatchCollection = Regex.Matches(text, expr) Dim m As Match For Each m In mc Console.WriteLine(m) Next m End Sub Sub Main() Dim str As String = "A Thousand Splendid Suns" Console.WriteLine("Matching words that start with 'S': ") showMatch(str, "SS*") Console.ReadKey() End Sub End Module 当上述代码被编译和执行时,它产生了以下结果: Matching words that start with 'S': The Expression: SS* Splendid Suns 例2以下示例匹配以“m”开头并以“e”结尾的单词: Imports System.Text.RegularExpressions Module regexProg Sub showMatch(ByVal text As String, ByVal expr As String) Console.WriteLine("The Expression: " + expr) Dim mc As MatchCollection = Regex.Matches(text, expr) Dim m As Match For Each m In mc Console.WriteLine(m) Next m End Sub Sub Main() Dim str As String = "make a maze and manage to measure it" Console.WriteLine("Matching words that start with 'm' and ends with 'e': ") showMatch(str, "mS*e") Console.ReadKey() End Sub End Module 当上述代码被编译和执行时,它产生了以下结果: Matching words start with 'm' and ends with 'e': The Expression: mS*e make maze manage measure 例3此示例替换了额外的空白空间: Imports System.Text.RegularExpressions Module regexProg Sub Main() Dim input As String = "Hello World " Dim pattern As String = "s+" Dim replacement As String = " " Dim rgx As Regex = New Regex(pattern) Dim result As String = rgx.Replace(input, replacement) Console.WriteLine("Original String: {0}", input) Console.WriteLine("Replacement String: {0}", result) Console.ReadKey() End Sub End Module 当上述代码被编译和执行时,它产生了以下结果: Original String: Hello World Replacement String: Hello World |
请发表评论