Regex正则表达式之“\d+”、RegexOptions.IgnoreCase、Group
原文地址:http://www.dotnetperls.com/regex-match-vbnet
Regex. For testing and manipulating text,the Regex class is useful. With Regex,we use a text-processing language. This language easily handles string data. 正则表达式。Regex类对于用来测试和操作文本是有用的。使用正则表达式方便我们使用文本处理语言。这种语言很容易处理字符串数据。
Functions. With Match,we search strings. And with Replace,we change those we find. And often RegexOptions are used to change how these functions are evaluated. 功能。我们用Match方法搜索字符串。并用Replace替换改变那些我们发现需要改变的量。regexoptions经常用来改变这些功能求值。
Match. This program uses Regex. Please notice the System.Text.RegularExpressions namespace. The Regex pattern "d+" matches one or more digit characters together. Match这个方法用到了正则表达式,要注意System.Text.RegularExpressions的命名空间。正则标识符"d+" 能够匹配到一个或者在一起的几个数字字符。
Success: We test if the match is successful. If it is,we print (with Console.WriteLine) its value—the string "77." 成功的方法: 我们测试看Match这个方法是否使用成功了。如果成功,我们就会打印出结果——字符串“77”。
Based on: .NET 4.5 基于平台版本.NET 4.5
下面是代码:(代码就不需要翻译了) 例一:“d+”的用法 VB.NET program that uses Regex
Imports System.Text.RegularExpressions Module Module1 Sub Main() Dim regex As Regex = New Regex("d+")//正则标识符"d+" 能够匹配到一个或者在一起的几个数字字符。 Dim match As Match = regex.Match("Dot 77 Perls") If match.Success Then Console.WriteLine(match.Value) End If End Sub End Module
Output 77 下面是代码:(代码就不需要翻译了)
IgnoreCase. Next,we use different syntax,and an option,for Match. We call the Regex.Match shared Function—no Regex object is needed. We then specify an option,RegexOptions.IgnoreCase. 忽略大小写的方法。 接下来,我们使用不同的语法,和一个选项以匹配。我们称之为正则表达式匹配共享功能。不需要正则表达式对象。然后我们指定一个选项,regexoptions.ignorecase。
IgnoreCase: 忽略大小写:
下面是代码: 例二:RegexOptions.IgnoreCase的用法 VB.NET program that uses RegexOptions.IgnoreCase
Imports System.Text.RegularExpressions
Module Module1
Sub Main() ' Match ignoring case of letters. Dim match As Match = Regex.Match("I like that cat", "C.T", RegexOptions.IgnoreCase) If match.Success Then ' Write value. Console.WriteLine(match.Value) End If End Sub
End Module
Output cat Groups. This example uses Match and Groups. We specify the case of letters is unimportant with RegexOptions.IgnoreCase. And finally we test for Success on the Match object received. 群组的方法。 这个例子使用了Match 和 Groups 两个方法。我们指定用RegexOptions.IgnoreCase的字母无关紧要。最后我们成功接收了匹配结果。
Info: 信息:
群体索引: 下面是代码: 例三:Groups的用法
VB.NET program that uses Regex.Match
Imports System.Text.RegularExpressions
Module Module1
Sub Main() ' The input string. Dim value As String = "/content/alternate-1.aspx"
' Invoke the Match method. Dim m As Match = Regex.Match(value,_ "content/([A-Za-z0-9-]+).aspx$",_ RegexOptions.IgnoreCase)//这句话表示将content目录下文件种类为aspx的文件名放到一个组里面
' If successful,write the group. If (m.Success) Then Dim key As String = m.Groups(1).Value Console.WriteLine(key) End If End Sub
End Module
Output
alternate-1 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |