Man, this is really close:
Imports str = System.Text.RegularExpressions
Private Function x(ByVal intext As String) As String
Dim out As String
Dim rgx As New
str.Regex("(?<before>.*)(?<src>src="
".*"")(?<after>.*)",
str.RegexOptions.Singleline)
For Each m As str.Match In rgx.Matches(intext)
out &= m.Groups("before").ToString _
&
m.Groups("src").ToString.Replace("\"
;, "/") _
& m.Groups("after").ToString
Next
Return out
End Function
For some reason I don't understand, it's not replacing the
backslashes on that last src="" line. I don't
have any more time to play w/this, unfortunately.
In real life, that 'out' var should be a stringbuilder I
guess...
________________________________
From: Discussion of writing applications and components
using Visual Basic .NET on behalf of Rod Stephens
Sent: Sat 3/11/2006 7:18 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Ū 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
|