Tamal Banerjee
Tamal Banerjee

Reputation: 503

How to find a certain pattern using regular expressions in vb.net

I'm completely new VB.net regular expressions, however I know Perl regex pretty well, but when I tried to use perl regex type find and replaces it doesn't work. I'm trying to replace all

"figure digit", "section digit", "table digit" expressions with ignorecase

which in perl is like

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

How do I achieve this in vb.net

Upvotes: 0

Views: 95

Answers (1)

Sehnsucht
Sehnsucht

Reputation: 5049

Here's an example of using Regex in VB.Net

' 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)"

Upvotes: 1

Related Questions