Your technology certification is waiting. Enroll in Cloud Class ®
source = System.Text.RegularExpressions.Regex.Replace(source, @"(<a\s+[^>]*href=[""']?)http://urltomyside\.com/", "$1http://urltoanyotherside.com", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// ( ... ) - capturing parentheses
// <a - find a literal "<a"
// \s+ - find one or more ( + ) whitespace ( \s ) characters
// [^>]* - find zero or more ( * ) of any character NOT ( [^ ...] ) a closing bracket ( > )
// href= - find a literal "href="
// [""']? - find zero or one ( ? ) of either a double- or single-quote ( ["'] ); there are two double-quotes because it has to be escaped for C#
// http://urltomyside\.com - find the url; Note, the dot ( . ) has to be escaped ( \. ) for the pattern because it is a special character in regex
// In the replace, you put the replacement URL as normal
// (i.e. no special characters); however, we inclue
// $1 at the beginning of it so that the text we captured
// with the parentheses described above is inserted with
// the replacement URL. If we don't include the $1, then
// you will erase the "<a>" up to where the old URL is found.
Open in new window
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Please enter a first name
Please enter a last name
Must be at least 4 characters long.
Join and Comment
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.