如何在vb.net中使用正则表达式查找某个模式

时间:2016-08-04 15:13:09

标签: regex vb.net

我是全新的VB.net正则表达式,但我知道Perl正则表达式相当不错,但是当我尝试使用perl正则表达式查找并替换它时不起作用。我正在尝试替换所有

  

“figure digit”,“section digit”,“table digit”表达式,包含ignorecase

在perl中就像

Find "figure (\d+)"
Replace "<a href="fig$1">figure $1</href>"
Find "table (\d+)"
Replace "<a href="tab$1">table $1</href>" and so on

我如何在vb.net中实现这一目标

1 个答案:

答案 0 :(得分:1)

以下是在VB.Net中使用Regex的示例

' Top of the file

Imports System.Text.RegularExpressions

' Inside a method (for example)

' declares and initialize a regex object
Dim regexObj As New Regex("(figure|section|table) (\d+)", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
' Compiled option is strictly needed here (good when reusing same regex)

' some sample text
Dim input = "some text with a figure 10 or a section 20 or a table 30"

Dim output = regexObj.Replace(input, "[$1] ($2)")
' output will be
' "some text with a [figure] (10) or a [section] (20) or a [table] (30)"