Here is one I use to replace server image paths with a
content id for HTML
mail with embedded images. It uses the MatchEvaluator
delegate.
Function ProcessMessage(ByVal msgBody As String) As String
Dim newBody As String
Dim regexPattern As String =
"src[^>]*[^/].(?:jpg|bmp|gif|tif)(?:\""
;|\')"
Dim options As RegexOptions =
((RegexOptions.IgnorePatternWhitespace
Or RegexOptions.Multiline) _
Or RegexOptions.IgnoreCase)
Dim reg As Regex = New Regex(regexPattern, options)
newBody = reg.Replace(msgBody, New
MatchEvaluator(AddressOf
ReplaceImageLinks))
Return newBody
End Function
Function ReplaceImageLinks(ByVal m As Match) As String
Dim cidGuid As Guid
Dim contentID As String
cidGuid = Guid.NewGuid
contentID = cidGuid.ToString("N")
Return "src=""cid:" &
contentID & """"
End Function
End Module
-----Original Message-----
From: Rod Stephens [mailto:mstephens3 MSN.COM]
Sent: Saturday, March 11, 2006 9:19 AM
To: VBDOTNET DISCUSS.DEVELOP.COM
Subject: Re: [VBDOTNET] How to replace all occurences of
character within a
section of string.
> I did figure out a not so pretty way of
> doing it. Oh well. I guess it does work...
>
> Private Function FixSlashInPath(ByVal thisfile As
String) As String
> Dim iLoop As Integer
> Dim tempString As String
> Dim newString As String
> For iLoop =3D 0 To thisfile.Length - 5
> If thisfile.Substring(iLoop, 3) =3D
"src" Then
> tempString =3D thisfile.Substring(iLoop
+ 5,
> thisfile.IndexOf(Chr(34), iLoop + 6) - (iLoop + 5))
> newString =3D
tempString.Replace("\", "/")
> thisfile =3D
thisfile.Replace(tempString, newString)
> End If
> Next
> Return thisfile
> End Function
Here's a more regular-expression-ey attempt. It searches
for the pattern
"[^"]*" (note that the leading and
trailing quotes are part of the pattern),
which matches a quote, followed by any number of non-quotes,
followed by a
quote. The code loops through the matches making the
replacements. I
replaced / with ? in the example to better see what it was
doing.
Private Sub btnReplace_Click(ByVal sender As System.Object,
ByVal e As
System.EventArgs) Handles btnReplace.Click
Dim input As String = txtInput.Text
Dim result As String = ""
Dim next_unmatched As Integer = 0
For Each match As Match In Regex.Matches(input,
txtPattern.Text)
' Add the text before the next match.
If match.Index > next_unmatched Then
result &= input.Substring(next_unmatched,
match.Index -
next_unmatched)
End If
' Add the matched text.
result &= match.Value.Replace("/",
"?")
' Debug.WriteLine("[" & match.Value
& "]")
' Set next_unmatched to the index
' of the last character in this match.
next_unmatched = match.Index + match.Length
Next match
' Add the rest of the string.
If input.Length > next_unmatched Then
result &= input.Substring(next_unmatched)
End If
' Display the result.
txtResult.Text = result
End Sub
You can download an example at:
http://www.vb-helper.com/howto_net_replace_regex
_substrings.html
Unfortunately it's not much better than your code, although
it looks for
matched quotes rather than relying on the "src"
in the data. I couldn't
think of a way to make it substitute within the matches
automatically. I'd
be interested to see it.
Rod
Rod Stephens, Author of "Visual Basic 2005
Programmer's Reference"
http://www.v
b-helper.com/vb_prog_ref.htm
Join the VB Helper mailing list at http://www.v
b-helper.com/newsletter.html
===================================
This list is hosted by DevelopMentor(r) http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
===================================
This list is hosted by DevelopMentorŪ http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
|