.net - 如何从 vb.net 中的正则表达式匹配中获取字符串?

标签 .net regex vb.net

我有 :

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委托(delegate)处理每场比赛,然后调用getmonth功能。 RegEx.Replace函数将使用委托(delegate)替换每个匹配项。

关于.net - 如何从 vb.net 中的正则表达式匹配中获取字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6898740/

相关文章:

c - 如何在 "main"正则表达式搜索的每个匹配组中进行子正则表达式替换?

c# - 是否可以向 C# 或 VB.NET 添加关键字?

c# - 使用 JSON.Net 反序列化 $ref 文档指针

c# - 我们可以在 Windows 环境中使用 MonoTouch 开发 iOS 应用程序吗?

c# - 无法将 System.Collections.Generic List<T> 转换为 T

MYSQL错误: #3699 - Timeout exceeded in regular expression match

c# - .net 中的 IL 和堆栈实现?

java - 正则表达式从全名中删除首字母缩写

vb.net - Datagridview 列单击/排序

c# - 在显示给用户后立即删除动态生成的 PDF 文件