I have :
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
I think I hav开发者_Go百科e explained my problem properly..
So, can you please help ?
Here is what you want, I think.
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
This uses the GetMonthFromMatch
delegate to process each match and in turn call the getmonth
function. The RegEx.Replace
function will use the delegate to substitute each match.
First slightly modify your regex:
System.Text.RegularExpressions.Regex("###MONTH([+-][0-9])###")
As you can see, I've just put number and +/- in parentheses. This is so that we can retrieve them later.
So now you can access just data you need (e.g. -3) whit this line of code:
match.Groups(1).Value
EDIT:
There's even an easier way :) Just use Replace function.
In your example it would go like this:
Dim regex = New System.Text.RegularExpressions.Regex("###MONTH([+-][0-9])###")
regex.Replace(Text, "getmonth($1)")
$1 references to first parentheses in the regular expression, so instead of $1 there will be what ever month it actually is.
精彩评论