课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

VB.Net - 正则表达式

当前位置:免费教程 » 程序设计 » VB.Net

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


定义正则表达式的构造

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

正则表达式类

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

正则表达式类有以下常用方法:

SN方法和说明
1

Public Function IsMatch (input As String) As Boolean
公共函数IsMatch(输入作为字符串)作为布尔

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

2

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

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

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

3

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

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

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

4

Public Function Matches (input As String) As MatchCollection

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

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

5

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

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

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

6

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

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

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

有关方法和属性的完整列表,请参阅Microsoft文档。

示例1

以下示例匹配以“S”开头的单词:

  1. Imports System.Text.RegularExpressions
  2. Module regexProg
  3. Sub showMatch(ByVal text As String, ByVal expr As String)
  4. Console.WriteLine("The Expression: " + expr)
  5. Dim mc As MatchCollection = Regex.Matches(text, expr)
  6. Dim m As Match
  7. For Each m In mc
  8. Console.WriteLine(m)
  9. Next m
  10. End Sub
  11. Sub Main()
  12. Dim str As String = "A Thousand Splendid Suns"
  13. Console.WriteLine("Matching words that start with 'S': ")
  14. showMatch(str, "SS*")
  15. Console.ReadKey()
  16. End Sub
  17. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. Matching words that start with 'S':
  2. The Expression: SS*
  3. Splendid
  4. Suns


例2

以下示例匹配以“m”开头并以“e”结尾的单词:

  1. Imports System.Text.RegularExpressions
  2. Module regexProg
  3. Sub showMatch(ByVal text As String, ByVal expr As String)
  4. Console.WriteLine("The Expression: " + expr)
  5. Dim mc As MatchCollection = Regex.Matches(text, expr)
  6. Dim m As Match
  7. For Each m In mc
  8. Console.WriteLine(m)
  9. Next m
  10. End Sub
  11. Sub Main()
  12. Dim str As String = "make a maze and manage to measure it"
  13. Console.WriteLine("Matching words that start with 'm' and ends with 'e': ")
  14. showMatch(str, "mS*e")
  15. Console.ReadKey()
  16. End Sub
  17. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. Matching words start with 'm' and ends with 'e':
  2. The Expression: mS*e
  3. make
  4. maze
  5. manage
  6. measure


例3

此示例替换了额外的空白空间:

  1. Imports System.Text.RegularExpressions
  2. Module regexProg
  3. Sub Main()
  4. Dim input As String = "Hello World "
  5. Dim pattern As String = "s+"
  6. Dim replacement As String = " "
  7. Dim rgx As Regex = New Regex(pattern)
  8. Dim result As String = rgx.Replace(input, replacement)
  9. Console.WriteLine("Original String: {0}", input)
  10. Console.WriteLine("Replacement String: {0}", result)
  11. Console.ReadKey()
  12. End Sub
  13. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. Original String: Hello World
  2. Replacement String: Hello World
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号