Regex正则表达式之Shared、NextMatch、IsMatch. (二)

前端之家收集整理的这篇文章主要介绍了Regex正则表达式之Shared、NextMatch、IsMatch. (二)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原文地址:http://www.dotnetperls.com/regex-match-vbnet

Shared.

A Regex object requires time to be created. We can instead share Regex objects,with the shared keyword. A shared Regex object is faster than shared Regex Functions.

共享方法

一个正则表达式对象需要时间去创造。我们可以用Shared关键字分享regex对象。共享Regex对象的速度比共享的正则表达式方法的速度快。

Therefore:
Storing a Regex as a field in a module or class often results in a speed boost,when Match is called more than once.

因此:
当不止一次调用Match方法时,存储一个正则表达式作为一个模块或类可以加快速度。


Function:
The Match function is an instance function on a Regex object. This program has the same result as the prevIoUs program.

功能

Match方法变为一个正则表达式实例化后的一个方法。这个程序与以前的程序相同。

下面是代码

例四:Shared. 方法

VB.NET program that uses Match on Regex field


Imports System.Text.RegularExpressions


Module Module1


@H_403_53@ ''' <summary>

''' Member field regular expression.

''' </summary>

Private _reg As Regex = New Regex("content/([A-Za-z0-9\-]+)\.aspx$",_

RegexOptions.IgnoreCase)


Sub Main()

@H_403_53@' The input string.

Dim value As String = "/content/alternate-1.aspx"


@H_403_53@' Invoke the Match method.

' ... Use the regex field.

Dim m As Match = _reg.Match(value)


@H_403_53@' 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

就是把原来的 Regex.Match方法改为new一个Regex

_reg As Regex = New Regex("content/([A-Za-z0-9\-]+)\.aspx$",_

RegexOptions.IgnoreCase)


Match,NextMatch.

The Match() Function returns the first match only. But we can call NextMatch() on that returned Match object. This is a match that is found in the text,further on.

Match,NextMatch.方法

Match方法只会返回第一个匹配值,但我们可以使用NextMatch方法在第一个匹配的返回值中再次匹配。这是一个进一步在文本中找到的匹配项的方法


Tip:
NextMatch can be called in a loop. This results in behavior similar to the Matches method (which may be easier to use).

提示
可以在循环中调用NextMatch。此结果在行为类似于匹配方法(可能更容易使用)。

下面是代码

例五:NextMatch

VB.NET program that uses Match,NextMatch


Imports System.Text.RegularExpressions


Module Module1


Sub Main()

@H_403_53@' Get first match.

Dim match As Match = Regex.Match("4 and 5","\d")


If match.Success Then

Console.WriteLine(match.Value)

End If


@H_403_53@' Get next match.

match = match.NextMatch()


If match.Success Then

Console.WriteLine(match.Value)

End If

End Sub


End Module


Output


4

5

先匹配到4,再匹配到5


IsMatch.

This returns true if a String matches the regular expression. We get a Boolean that tells us whether a pattern matches. If no other results are needed,IsMatch is useful.

表示是否匹配的布尔值

如果正则表达式与字符串相匹配则返回true.我们用一个布尔值判断标识符是否匹配。如果没有其他需要的结果,IsMatch 就是有用的。


Here:
This program introduces the IsValid Boolean function,which computes the result of the Regex.IsMatch function on its parameter.
Note:
The regular expression pattern indicates any string of lowercase ASCII letters,uppercase ASCII letters,or digits.

在这里
这个函数介绍了有效的布尔函数,计算的结果regex.ismatch功能参数。
注意:
正则表达式模式表示任何字符串的小写字母,大写字母,或数字。

下面是代码

例六:IsMatch.

VB.NET program that uses Regex.IsMatch function


Imports System.Text.RegularExpressions


Module Module1

Function IsValid(ByRef value As String) As Boolean

Return @H_403_53@Regex.IsMatch(value,"^[a-zA-Z0-9]*$")

End Function


Sub Main()

Console.WriteLine(IsValid("dotnetperls0123"))

Console.WriteLine(IsValid("DotNetPerls"))

Console.WriteLine(IsValid(":-)"))

End Sub

End Module


Output


True

True

False


[a-zA-Z0-9]意思是范围包括小写字母a至z;大写字母A至Z;数字0至9

原文链接:https://www.f2er.com/regex/359978.html

猜你在找的正则表达式相关文章