[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 604
  • Last Modified:

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
0
kevp75
Asked:
kevp75
  • 4
  • 2
  • 2
  • +2
3 Solutions
 
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now