Regex正则表达式之“\d+”、RegexOptions.IgnoreCase、Groups(一)

来源:互联网 发布:广州数控编程实例 编辑:程序博客网 时间:2024/06/08 09:43

原文地址: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:
This enum value, a constant, specifies that lower and uppercase letters are equal.

忽略大小写:
这个枚举值是一个常数,指定较低的大写字母都是平等的。


下面是代码:

例二: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:
When we execute this program, we see the target text was successfully extracted from the input.

信息:
当我们执行这个程序,我们看到目标文本成功地从输入中提取出来了。


Groups index:
We use the value 1 to get the first group from the Match. With Regex, indexing starts at 1 not 0 (don't ask why).

群体索引:
我们使用的值1,以获得第一组从匹配。在正则表达式中,索引从1而不是0开始(别问我为什么)。

下面是代码:

例三: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






0 0