加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 07:14:44 所属栏目:百科 来源:网络整理
导读:我有 : 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,th
我有 :

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函数将使用委托替换每个匹配.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读