如何通过VB.NET正则表达式提取特定字符串内容?

2026-05-06 12:041阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计200个文字,预计阅读时间需要1分钟。

如何通过VB.NET正则表达式提取特定字符串内容?

我有:Dim Text=some text here

我有 :

Dim Text = "some text here ###MONTH-3### some text here ###MONTH-2### some text here" Dim regex = New System.Text.RegularExpressions.Regex("###MONTH[+-][0-9]###") For Each match In regex.Matches(Text) // What to write here ? // So, that ###MONTH-i### gets replaced with getmonth(i) // Therefore, final Text will be : // Text = "some text here" + getmonth(-3) + "some text here" + getmonth(-2) + "some text here" Next match

我想我已正确解释了我的问题..

那么,你能帮忙吗?

我想这就是你想要的.

Dim text As String = "some text here ###MONTH-3### some text here ###MONTH-2### ..." Dim regex = New System.Text.RegularExpressions.Regex("###MONTH[+-][0-9]###") return regex.replace(text, AddressOf GetMonthFromMatch) Function GetMonthFromMatch(ByVal m As Match) As String ' Get the matched string. Dim matchText As String = m.ToString() Dim offset As Int = Integer.Parse(matchText.Right(2)) Return getmonth(offset) End Function

这使用GetMonthFromMatch委托处理每个匹配,然后调用getmonth函数. RegEx.Replace函数将使用委托替换每个匹配.

如何通过VB.NET正则表达式提取特定字符串内容?

本文共计200个文字,预计阅读时间需要1分钟。

如何通过VB.NET正则表达式提取特定字符串内容?

我有:Dim Text=some text here

我有 :

Dim Text = "some text here ###MONTH-3### some text here ###MONTH-2### some text here" Dim regex = New System.Text.RegularExpressions.Regex("###MONTH[+-][0-9]###") For Each match In regex.Matches(Text) // What to write here ? // So, that ###MONTH-i### gets replaced with getmonth(i) // Therefore, final Text will be : // Text = "some text here" + getmonth(-3) + "some text here" + getmonth(-2) + "some text here" Next match

我想我已正确解释了我的问题..

那么,你能帮忙吗?

我想这就是你想要的.

Dim text As String = "some text here ###MONTH-3### some text here ###MONTH-2### ..." Dim regex = New System.Text.RegularExpressions.Regex("###MONTH[+-][0-9]###") return regex.replace(text, AddressOf GetMonthFromMatch) Function GetMonthFromMatch(ByVal m As Match) As String ' Get the matched string. Dim matchText As String = m.ToString() Dim offset As Int = Integer.Parse(matchText.Right(2)) Return getmonth(offset) End Function

这使用GetMonthFromMatch委托处理每个匹配,然后调用getmonth函数. RegEx.Replace函数将使用委托替换每个匹配.

如何通过VB.NET正则表达式提取特定字符串内容?