A regular expression (shortened as regex or regexp;[1] also referred to as rational expression[2][3]) is a sequence of characters that specifies a search pattern. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation. It is a technique developed in theoretical computer science and formal language theory.
private string ReplaceBlankTarget(string text) { string pattern = @"<a href=""https:\/\/(?<link>[^""]+)"" target=""_blank"">"; string substitution = @"<a href=""https:\/\/${link}"" target=""_self"">"; RegexOptions options = RegexOptions.Multiline; Regex regex = new Regex(pattern, options); return regex.Replace(text, substitution); }
string pattern = @"(<a.*?(?<href>href=\x22.*?\x22).*?>)"; string substitution = "<a ${href}>"; RegexOptions options = RegexOptions.Multiline; Regex regex = new Regex(pattern, options); string result = regex.Replace(input, substitution);
Additional information for Substitutions in Regular Expressions
This will capture the URL as well as the text.
string input = @"<a href=""\page1.htm"">Page 1</a><a href=""\page2.htm"">Page 2</a>";
var matches = Regex.Matches(input, @"<a\shref=""(?<url>.*?)"">(?<text>.*?)</a>");
foreach (Match m in matches)
{
Console.WriteLine("URL: " + m.Groups["url"].Value + "; Text: " + m.Groups["text"].Value);
}
Regular expression to select script tag:
(<script[\s\S]*?>[\s\S]*?<\/script>)