Regular Expression (RegEx)
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
Regular expression to select script tag:
(<script[\s\S]*?>[\s\S]*?<\/script>)
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);
}
When working with software versions like 1.0.2 or 3.45.12-SNAPSHOT, it's common to use regular expressions (regex) to validate or extract these patterns. This Snipp shows you a simple and effective way to match such version numbers using regex.
What We Want to Match:
1.0.23.32.343.45.12-SNAPSHOT
These are typical semantic version numbers and may optionally include a suffix like -SNAPSHOT.
The Regex:
^\d+\.\d+\.\d+(?:-SNAPSHOT)?$
How It Works:
^— Start of the string\d+— One or more digits (major, minor, patch)\.— Dots separating version parts(?:-SNAPSHOT)?— Optional suffix (non-capturing group)$— End of the string
Matches:
- ✔️
1.0.2 - ✔️
3.32.34 - ✔️
3.45.12-SNAPSHOT
Not Matched:
- ❌
1.0 - ❌
3.2.4-RELEASE - ❌
a.b.c
This pattern is useful when validating software version inputs in forms, logs, or build scripts.
Comments