Just Me,
Here is a quick Find & Replace class based on my other sample:
Public Class FindReplace
Private m_parser As Regex
Private m_replacement As String
Private m_input As String
Private m_output As StringBuilder
Private m_startIndex As Integer
Private m_match As Match
Public Property Pattern() As String
Get
Return m_parser.ToString()
End Get
Set(ByVal value As String)
m_parser = New Regex(value, RegexOptions.Compiled)
End Set
End Property
Public Property Replacement() As String
Get
Return m_replacement
End Get
Set(ByVal value As String)
m_replacement = value
End Set
End Property
Public Property Input() As String
Get
Return m_input
End Get
Set(ByVal value As String)
m_input = value
m_output = New StringBuilder
m_startIndex = 0
End Set
End Property
Public ReadOnly Property Output() As String
Get
Return m_output.ToString()
End Get
End Property
Public Function FindNext() As Boolean
If m_match Is Nothing Then
m_match = m_parser.Match(m_input)
Else
m_match = m_match.NextMatch()
End If
Return m_match.Success
End Function
Public Function Replace() As Boolean
If Not m_match.Success Then
FindNext()
End If
If m_match.Success Then
m_output.Append(m_input, m_startIndex, m_match.Index -
m_startIndex)
m_output.Append(m_parser.Replace(m_match.Value,
m_replacement))
m_startIndex = m_match.Index + m_match.Length
End If
Return m_match.Success
End Function
Public Overrides Function ToString() As String
Return m_output.ToString()
End Function
End Class
Public Sub Main()
Dim find As New FindReplace
find.Pattern = "(?<key>\w+)=(?<value>\w+)(:;|)"
find.Replacement = "Key ${key} has value ${value}"
find.Input = "a=1;b=2;c=3;d=4;e=5;"
Do While find.Replace()
Loop
Debug.WriteLine(find.ToString(), "output")
End Sub
Alternate loop that should also work:
Do While find.FindNext()
find.Replace()
Loop
Hope this helps
Jay
" Just Me" <gr****@a-znet.com> wrote in message
news:O7**************@TK2MSFTNGP10.phx.gbl...
Alternatively you could iterate the matches [5] and use the index [6] and
length [7] properties to act on the original string.
This is what I do now. However, I simply replace the found string with a
replace string.
That is, I do not allow the regular expression type replacements using
groups of characters found in the search. That's what I can't find out how
to do.
Thanks