I need to use C# Regex to do a link rewrite for html pages and I need to replace the links enclosed with quotes (“) with my own ones. Say for example, I need to replace the following
"slashdot.org/index.rss"
into
"MY_OWN_LINK"
However, the actual link can be of the form
"//slashdot.org/index.rss" or
"/slashdot.org/index.rss"
where there can be other values that comes before "slashdot.org/index.rss" but after the quote (“) which I don’t care about.
To summarize, as long as the link ends with "slashdot.org/index.rss", I would want to replace the entire link with "MY_OWN_LINK".
How can I use Regex.Replace for the above?
edit: updated answer according to comment.
First, you don’t have to use a regular expression for this job. Just check whether or not the string ends with `”slashdot.org/index.rss”‘, and if it is, replace the entire string.
If you’re using regular expression, you’d better just test whether or not the string ends with
"slashdot.org/index.rss"and act accordingly, like so:If you insist of using
Regex.Replace, go forwhere the
^and the$stands for line/string begin/end respectively. The first.*means “capture the start of the URL, whatever it is”. The last dot is perpended with slash, as it usually means “any character”.For additional info, see this cheat sheet of regular expression in C#.