vb.net正则表达式html,VB.Net - 正则表达式

正则表达式是可以与输入文本匹配的模式。 .Net框架提供了允许这种匹配的正则表达式引擎。 模式由一个或多个字符文字,运算符或构造组成。

定义正则表达式的构造

有各种类别的字符,运算符和构造,允许您定义正则表达式。 单击以下链接以查找这些结构。

正则表达式类

正则表达式类用于表示一个正则表达式。

正则表达式类有以下常用方法:SN方法和说明

1Public Function IsMatch (input As String) As Boolean

公共函数IsMatch(输入作为字符串)作为布尔

表示在正则表达式构造函数中指定的正则表达式是否发现在指定的输入字符串匹配。

2Public Function IsMatch (input As String, startat As Integer ) As Boolean

公共函数IsMatch(输入作为字符串,startat作为整数)作为布尔

指示在Regex构造函数中指定的正则表达式是否在指定的输入字符串中找到匹配项,从字符串中指定的起始位置开始。

3Public Shared Function IsMatch (input As String, pattern As String ) As Boolean

公共共享函数IsMatch(输入作为字符串,图案作为字符串)作为布尔

指示指定的正则表达式是否在指定的输入字符串中找到匹配项。

4Public Function Matches (input As String) As MatchCollection

公共函数匹配(输入作为字符串)作为MatchCollection

搜索指定的输入字符串以查找正则表达式的所有出现。

5Public Function Replace (input As String, replacement As String) As String

公共函数替换(输入作为字符串,更换作为字符串)作为字符串

在指定的输入字符串中,使用指定的替换字符串替换与正则表达式模式匹配的所有字符串。

6Public Function Split (input As String) As String()

公共函数(输入作为字符串)作为字符串()

将输入字符串插入到由正则表达式构造函数中指定一个正则表达式模式定义的位置的子字符串数组。

有关方法和属性的完整列表,请参阅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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值