Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
after use webclient to get downloadstring, i wish to replace all the href part with something new, plz help
Posted
Comments
So, what have you tried and what is the issue?
風知鈴音 28-Mar-15 2:13am    
i tried some Regex code from internet, but not really do the job.....i actually dont know how regex works, just want a quick solution of this..
Try Abhinav's answer below.
風知鈴音 28-Mar-15 2:42am    
i also need to replace the url inside the href...

String.Replace[^] will help you replace string with other text.
 
Share this answer
 
Use Regex.Replace[^]
Try this:
C#
using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        // original string
        string input = "<a href='http://oldurl.com'>somewhere</a>";
        // use Regex.Replace to replace the pattern in the input.
        string pattern = @"(?<=href=[']).+(?=')";
        string output = Regex.Replace(input, pattern,"http://newurl.com");
        // Write the output.
        Console.WriteLine(input);
        Console.WriteLine(output);
    }
}

It will output:
XML
<a href='http://oldurl.com'>somewhere</a>
<a href='http://newurl.com'>somewhere</a>

Learn more about Regex[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900