Regex.Replace

I find that I am in need of some help with this one...

I need to find all occurances of a pattern.   Once found I need to replace all but the last occurance with a blank, and replace the last occurance with a different string.

Any thoughts?

Here's the string:

tmpString = Regex.Replace(tmpString, "<link .*?href=""(.*?)"" />", "<style type=""text/css"">" & FileContents & "</style>")


It will find all occurances of the <link href> tag for stylesheets, what I need to do is if there are 3 of them in the page, replace the first 2 with "", but replace the 3rd with the "<style..." as above
LVL 25
kevp75Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

strickddCommented:
You can use the RegEx.Match method (http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx).

This will find all occurrences and all you to loop through and do a replace. Then you simply stop one short of the total count.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Göran AnderssonCommented:
Use Regex.Matches to get a match collection for the occurances. Then you can replace each occurance in a loop.

If you loop through the occurances backwards, it's easier to change the string, as one change doesn't affect the postion of the next change to do.
0
kevp75Author Commented:
tried that, but when I use Regex.Replace inside that loop it still replaces all occurences
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

kevp75Author Commented:
How?  Example please?   It seems what I am doing for the loop still replaces all occurences
0
strickddCommented:
You don't need to use the RegEx.Replace inside the loop, that's part of the problem. You want to loop through the matches which give you references to the string that satisfies the RegEx you specify. Then you replace those one by one.
0
kevp75Author Commented:
With string.Replace not Regex.Replace?

Do you have an example?
0
Tech_StigCommented:
I could be wrong but I don't think you can do that with a regular expression. What I would do is one of two things:
Ex1.
 - Create a list of strings with all occurrences
 - Loop through the strings and do the replacement as necessary
 - output the list.

Ex2.
 - Loop through the file and count the occurrences and hole that in a variable.
 - loop back through the file counting as you are making the changes. When replaceCount == (totalCount - 1) then make the final change.
0
käµfm³d 👽Commented:
Something like:
Dim test As String = "<link href=""jimmy.html"" /><link href=""johnny.html"" /><link href=""joey.html"" />"
Dim pattern As String = "<link .*?href=""(.*?)"" />"
Dim matches As MatchCollection
Dim i As Integer

matches = Regex.Matches(test, pattern)

For i = matches.Count - 1 To 1 Step -1
    test = String.Concat(test.Substring(0, matches(i).Index), test.Substring(matches(i).Index + matches(i).Length))
Next

test = String.Concat(test.Substring(0, matches(i).Index), "<style type=""text/css"">", FileContents, "</style>", test.Substring(matches(i).Index + matches(i).Length))

Open in new window

0
kevp75Author Commented:
Got it.

Much appreciated
Dim tmp As MatchCollection
tmp = Regex.Matches(tmpString, "<link .*?href=""(.*?)"" />")
Dim tmpCt As Long = 0
  For Each tmpS As Match In tmp
      tmpCt += 1
      If tmpCt = tmp.Count Then
         tmpString = tmpString.Replace(tmpS.Groups(0).ToString(), "<style type=""text/css"">" & FileContents & "</style>")
      Else
         tmpString = tmpString.Replace(tmpS.Groups(0).ToString(), "")
      End If
   Next

Open in new window

0
käµfm³d 👽Commented:
10 seconds too late...     *sigh*
0
Göran AnderssonCommented:
No, don't use replace in the loop. If the last match looks the same as any previous, you will have removed it before getting to the last match.

You can use a StringBuilder to build the result from unchanged parts of the original string and replaced strings:
Dim links As MatchCollection = Regex.Matches(tmpString, "<link .*?href=""(.*?)"" />")
Dim tmpCt As Integer = 0
Dim index As Integer = 0
Dim result As New StringBuilder()
For Each link As Match In links
   result.Append(tmpString, index, link.Index - index)
   tmpCt += 1
   If tmpCt = tmp.Count Then
      result.Append("<style type=""text/css"">" & FileContents & "</style>")
   End If
   index = link.Index + link.Length
Next
result.Append(tmpString, index, tmpString.Length - index)
tmpString = result.ToString()

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.