{"id":217,"date":"2011-01-05T01:42:42","date_gmt":"2011-01-05T08:42:42","guid":{"rendered":"https:\/\/wordpress-1325650-4848760.cloudwaysapps.com\/?p=217"},"modified":"2025-05-02T11:50:39","modified_gmt":"2025-05-02T15:50:39","slug":"regular-expression-c-sharp","status":"publish","type":"post","link":"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp","title":{"rendered":"C# Regular Expression: The Ultimate Beginner&#8217;s Guide"},"content":{"rendered":"\n<p>In this comprehensive guide, I&#8217;ll walk you through everything you need to know about C# regular expressions to supercharge your string processing and pattern-matching capabilities. If you are new to this domain, I would highly recommend going over <a href=\"https:\/\/codesamplez.com\/programming\/regular-expression-basics\" target=\"_blank\" rel=\"noreferrer noopener\">regular expression basics<\/a> first to learn the fundamentals in a language-agnostic way. <\/p>\n\n\n\n<p>Every major programming language supports regex, but the beauty of C# is that the .NET Framework provides a unified regex engine that works consistently across all .NET languages. Without further ado, let&#8217;s dive in!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Getting Started with C# Regular Expressions<\/h2>\n\n\n\n<p>Before diving into the practical examples, you need to know that all regex functionality in C# comes from the <code>System.Text.RegularExpressions<\/code> namespace. This namespace contains several crucial classes that will become your best friends:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">using<\/span> <span class=\"hljs-selector-tag\">System<\/span><span class=\"hljs-selector-class\">.Text<\/span><span class=\"hljs-selector-class\">.RegularExpressions<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The primary classes you&#8217;ll work with include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Regex<\/strong>: The main class for pattern matching operations<\/li>\n\n\n\n<li><strong>Match<\/strong>: Represents a single match result<\/li>\n\n\n\n<li><strong>MatchCollection<\/strong>: Stores multiple match results<\/li>\n\n\n\n<li><strong>Group<\/strong>: Represents a captured group within a match<\/li>\n\n\n\n<li><strong>GroupCollection<\/strong>: Contains multiple Group objects<\/li>\n\n\n\n<li><strong>RegexOptions<\/strong>: An enum with options to modify regex behavior<\/li>\n<\/ul>\n\n\n\n<p>The <code>RegexOptions<\/code> enum gives you tremendous flexibility in how your regex patterns work. Some commonly used options include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>IgnoreCase<\/strong>: Makes pattern matching case-insensitive<\/li>\n\n\n\n<li><strong>Multiline<\/strong>: Changes how <code>^<\/code> and <code>$<\/code> match at line breaks<\/li>\n\n\n\n<li><strong>Compiled<\/strong>: Compiles the regex to an assembly for faster execution<\/li>\n\n\n\n<li><strong>RightToLeft<\/strong>: Searches from right to left instead of the default left to right<\/li>\n\n\n\n<li><strong>ExplicitCapture<\/strong>: Only captures groups with explicit names or numbers<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Using the C# Regex Class<\/h2>\n\n\n\n<p>The Regex class is where all the regex magic happens. You have two primary ways to use it:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Method 1: Instantiate a Regex Object<\/h3>\n\n\n\n<p>When you need to use the same pattern multiple times, create a Regex instance first:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ Create a regex pattern for email validation<\/span>\nRegex emailRegex = <span class=\"hljs-keyword\">new<\/span> Regex(@<span class=\"hljs-string\">\"^&#91;A-Z0-9._%+-]+@&#91;A-Z0-9.-]+\\.&#91;A-Z]{2,}$\"<\/span>, RegexOptions.IgnoreCase);\n\n<span class=\"hljs-comment\">\/\/ Test multiple emails<\/span>\nstring&#91;] emails = { \n    <span class=\"hljs-string\">\"&#91;email protected]\"<\/span>, \n    <span class=\"hljs-string\">\"&#91;email protected]\"<\/span>, \n    <span class=\"hljs-string\">\"invalid-email\"<\/span> \n};\n\n<span class=\"hljs-keyword\">foreach<\/span> (string email in emails)\n{\n    Match match = emailRegex.Match(email);\n    Console.WriteLine($<span class=\"hljs-string\">\"{email}: {match.Success}\"<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This approach is more efficient when you&#8217;re reusing the same pattern because C# only needs to compile the regex pattern once.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Method 2: Use Static Methods<\/h3>\n\n\n\n<p>For one-off pattern matching, you can use the static methods:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">string&#91;] emails = { \n    <span class=\"hljs-string\">\"&#91;email protected]\"<\/span>, \n    <span class=\"hljs-string\">\"invalid.email.com\"<\/span>, \n    <span class=\"hljs-string\">\"&#91;email protected]\"<\/span> \n};\n\nstring pattern = @<span class=\"hljs-string\">\"^&#91;A-Z0-9._%+-]+@&#91;A-Z0-9.-]+\\.&#91;A-Z]{2,}$\"<\/span>;\n\n<span class=\"hljs-keyword\">foreach<\/span> (string email in emails)\n{\n    bool isValid = Regex.IsMatch(email, pattern, RegexOptions.IgnoreCase);\n    Console.WriteLine($<span class=\"hljs-string\">\"{email}: {isValid}\"<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Finding Multiple Matches with MatchCollection<\/h2>\n\n\n\n<p>What if you need to extract all matches of a pattern from a string? The <code>Matches<\/code> method returns a <code>MatchCollection<\/code> containing all matches:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">string text = <span class=\"hljs-string\">\"Contact us at &#91;email protected] or &#91;email protected] for support.\"<\/span>;\nstring pattern = @<span class=\"hljs-string\">\"&#91;A-Z0-9._%+-]+@&#91;A-Z0-9.-]+\\.&#91;A-Z]{2,}\"<\/span>;\n\nMatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase);\n\nConsole.WriteLine($<span class=\"hljs-string\">\"Found {matches.Count} email addresses:\"<\/span>);\n<span class=\"hljs-keyword\">foreach<\/span> (Match match in matches)\n{\n    Console.WriteLine(match.Value);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Working with Groups and Captures<\/h2>\n\n\n\n<p>One of the most powerful features of regex is the ability to capture specific parts of a match using groups. Here&#8217;s how you can extract components from an email address:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">string email = <span class=\"hljs-string\">\"&#91;email protected]\"<\/span>;\nstring pattern = @<span class=\"hljs-string\">\"^(&#91;A-Z0-9._%+-]+)@(&#91;A-Z0-9.-]+)\\.(&#91;A-Z]{2,})$\"<\/span>;\n\nMatch match = Regex.Match(email, pattern, RegexOptions.IgnoreCase);\n\n<span class=\"hljs-keyword\">if<\/span> (match.Success)\n{\n    string username = match.Groups&#91;<span class=\"hljs-number\">1<\/span>].Value;\n    string domain = match.Groups&#91;<span class=\"hljs-number\">2<\/span>].Value;\n    string tld = match.Groups&#91;<span class=\"hljs-number\">3<\/span>].Value;\n    \n    Console.WriteLine($<span class=\"hljs-string\">\"Username: {username}\"<\/span>);\n    Console.WriteLine($<span class=\"hljs-string\">\"Domain: {domain}\"<\/span>);\n    Console.WriteLine($<span class=\"hljs-string\">\"TLD: {tld}\"<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can also use named groups for better readability:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">string pattern = @<span class=\"hljs-string\">\"^(?&lt;username&gt;&#91;A-Z0-9._%+-]+)@(?&lt;domain&gt;&#91;A-Z0-9.-]+)\\.(?&lt;tld&gt;&#91;A-Z]{2,})$\"<\/span>;\nMatch match = Regex.Match(email, pattern, RegexOptions.IgnoreCase);\n\n<span class=\"hljs-keyword\">if<\/span> (match.Success)\n{\n    Console.WriteLine($<span class=\"hljs-string\">\"Username: {match.Groups&#91;\"<\/span>username<span class=\"hljs-string\">\"].Value}\"<\/span>);\n    Console.WriteLine($<span class=\"hljs-string\">\"Domain: {match.Groups&#91;\"<\/span>domain<span class=\"hljs-string\">\"].Value}\"<\/span>);\n    Console.WriteLine($<span class=\"hljs-string\">\"TLD: {match.Groups&#91;\"<\/span>tld<span class=\"hljs-string\">\"].Value}\"<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Real-World C# Regex Example: Extracting Time Values<\/h2>\n\n\n\n<p>Let&#8217;s look at a practical example of using regex to extract time values from a string:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ Pattern to match time in format hh:mm:ss<\/span>\nRegex timeRegex = <span class=\"hljs-keyword\">new<\/span> Regex(@<span class=\"hljs-string\">\"\\b(&#91;01]?\\d|2&#91;0-3]):(&#91;0-5]\\d):(&#91;0-5]\\d)\\b\"<\/span>);\n\nstring text = <span class=\"hljs-string\">\"Server logs: Error at 03:45:21, Warning at 07:12:53, Critical at 23:59:59\"<\/span>;\nMatchCollection matches = timeRegex.Matches(text);\n\n<span class=\"hljs-keyword\">List<\/span>&lt;string&gt; timeValues = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-keyword\">List<\/span>&lt;string&gt;();\n<span class=\"hljs-keyword\">foreach<\/span> (Match match in matches)\n{\n    timeValues.Add(match.Value);\n    \n    <span class=\"hljs-comment\">\/\/ You can also access individual components (hours, minutes, seconds)<\/span>\n    string hours = match.Groups&#91;<span class=\"hljs-number\">1<\/span>].Value;\n    string minutes = match.Groups&#91;<span class=\"hljs-number\">2<\/span>].Value;\n    string seconds = match.Groups&#91;<span class=\"hljs-number\">3<\/span>].Value;\n    \n    Console.WriteLine($<span class=\"hljs-string\">\"Found time: {hours}:{minutes}:{seconds}\"<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Search and Replace with Regular Expressions<\/h2>\n\n\n\n<p>C# regex is fantastic for performing complex search and replace operations. The <code>Replace<\/code> method lets you replace matches with new text:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">string text = <span class=\"hljs-string\">\"Contact &#91;email protected] or &#91;email protected] for support.\"<\/span>;\nstring pattern = @<span class=\"hljs-string\">\"&#91;A-Z0-9._%+-]+@&#91;A-Z0-9.-]+\\.&#91;A-Z]{2,}\"<\/span>;\n\n<span class=\"hljs-comment\">\/\/ Basic replacement<\/span>\nstring anonymized = Regex.Replace(text, pattern, <span class=\"hljs-string\">\"&#91;EMAIL REDACTED]\"<\/span>, RegexOptions.IgnoreCase);\nConsole.WriteLine(anonymized);\n\n<span class=\"hljs-comment\">\/\/ Advanced replacement with a MatchEvaluator delegate<\/span>\nstring customReplacement = Regex.Replace(text, pattern, match =&gt; {\n    string email = match.Value;\n    string&#91;] parts = email.Split(<span class=\"hljs-string\">'@'<\/span>);\n    <span class=\"hljs-keyword\">return<\/span> parts&#91;<span class=\"hljs-number\">0<\/span>].Substring(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">2<\/span>) + <span class=\"hljs-string\">\"***@\"<\/span> + parts&#91;<span class=\"hljs-number\">1<\/span>];\n}, RegexOptions.IgnoreCase);\n\nConsole.WriteLine(customReplacement); <span class=\"hljs-comment\">\/\/ \"Contact jo***@example.com or su***@company.org for support.\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Performance Tips for C# Regular Expressions<\/h2>\n\n\n\n<p>Regular expressions are powerful, but can sometimes impact performance. Here are some tips to make your regex operations faster:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Compile patterns you use frequently<\/strong>: Use <code>RegexOptions.Compiled<\/code> when you&#8217;re creating a pattern you&#8217;ll use many times.<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">Regex compiledRegex = <span class=\"hljs-keyword\">new<\/span> Regex(pattern, RegexOptions.Compiled);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Use static regex instances<\/strong>: For patterns you use throughout your application, create static Regex instances.<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">static<\/span> readonly Regex EmailRegex = <span class=\"hljs-keyword\">new<\/span> Regex(@<span class=\"hljs-string\">\"^&#91;A-Z0-9._%+-]+@&#91;A-Z0-9.-]+\\.&#91;A-Z]{2,}$\"<\/span>, RegexOptions.IgnoreCase | RegexOptions.Compiled);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Use the <code>Match<\/code> method instead of <code>IsMatch<\/code> when you need the match details<\/strong>: If you need both validation and extraction, use <code>Match<\/code> directly.<\/li>\n\n\n\n<li><strong>Anchor your patterns<\/strong>: Use <code>^<\/code> and <code>$<\/code> when matching entire strings to avoid unnecessary backtracking.<\/li>\n\n\n\n<li><strong>Be specific<\/strong>: Make your patterns as specific as possible to avoid excessive backtracking.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Common C# Regex Patterns for Practical Use<\/h2>\n\n\n\n<p>Here are some regex patterns for common validation tasks in C#:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Email Validation<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">string emailPattern = @<span class=\"hljs-string\">\"^&#91;A-Z0-9._%+-]+@&#91;A-Z0-9.-]+\\.&#91;A-Z]{2,}$\"<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Phone Number (US Format)<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">string phonePattern = @<span class=\"hljs-string\">\"^\\(?(&#91;0-9]{3})\\)?&#91;-. ]?(&#91;0-9]{3})&#91;-. ]?(&#91;0-9]{4})$\"<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">URL Validation<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">string urlPattern = @<span class=\"hljs-string\">\"^(https?:\/\/)?(www\\.)?(&#91;a-zA-Z0-9-]+\\.)+&#91;a-zA-Z]{2,}(\/&#91;^\\s]*)?$\"<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Strong Password Validation<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">string passwordPattern = @<span class=\"hljs-string\">\"^(?=.*&#91;a-z])(?=.*&#91;A-Z])(?=.*\\d)(?=.*&#91;^\\da-zA-Z]).{8,}$\"<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Debugging C# Regular Expression<\/h2>\n\n\n\n<p>Debugging complex regex patterns can be challenging. Here are some tips:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Use online regex testers<\/strong>: Websites like regex101.com or regexr.com let you test and visualize your patterns.<\/li>\n\n\n\n<li><strong>Break complex patterns into smaller parts<\/strong>: Test each component separately before combining them.<\/li>\n\n\n\n<li><strong>Use verbose mode for complex patterns<\/strong>: The <code>RegexOptions.IgnorePatternWhitespace<\/code> option allows you to format your regex with whitespace and comments for readability.<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">string verbosePattern = @<span class=\"hljs-string\">\"\n    ^                       # Start of string\n    (?=.*&#91;a-z])            # Must contain lowercase\n    (?=.*&#91;A-Z])            # Must contain uppercase\n    (?=.*\\d)               # Must contain digit\n    (?=.*&#91;^\\da-zA-Z])      # Must contain special char\n    .{8,}                   # At least 8 chars long\n    $                       # End of string\n\"<\/span>;\n\nRegex passwordRegex = <span class=\"hljs-keyword\">new<\/span> Regex(verbosePattern, RegexOptions.IgnorePatternWhitespace);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s New in Modern .NET Regex<\/h2>\n\n\n\n<p>If you&#8217;re working with .NET 7 or later, you&#8217;ll benefit from significant regex performance improvements and new features:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Source Generators for Regex<\/strong>: This new feature can dramatically improve performance by generating specialized code at compile time.<\/li>\n\n\n\n<li><strong>Improved timeout handling<\/strong>: Better control over regex execution time limits.<\/li>\n\n\n\n<li><strong>RegexGenerator attribute<\/strong>: Allows compile-time regex validation and optimization.<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ In .NET 7+<\/span>\n&#91;GeneratedRegex(@<span class=\"hljs-string\">\"^&#91;A-Z0-9._%+-]+@&#91;A-Z0-9.-]+\\.&#91;A-Z]{2,}$\"<\/span>, RegexOptions.IgnoreCase)]\n<span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">static<\/span> partial Regex EmailRegex();\n\n<span class=\"hljs-comment\">\/\/ Usage<\/span>\n<span class=\"hljs-keyword\">if<\/span> (EmailRegex().IsMatch(input)) { &lt;em&gt;<span class=\"hljs-comment\">\/* ... *\/<\/span>&lt;\/em&gt; }<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Regular expressions in C# are an indispensable tool for any developer working with string data. They allow you to perform complex string operations with concise, powerful syntax. While they might seem intimidating at first, mastering regex will dramatically improve your ability to process and validate text data.<\/p>\n\n\n\n<p>Remember, the key to becoming proficient with C# regex is practice. Start with simple patterns and gradually work your way up to more complex ones. Use the provided examples as a starting point, and soon you&#8217;ll be writing sophisticated regex patterns to tackle any string matching challenge.<\/p>\n\n\n\n<p>For complete documentation, always refer to Microsoft&#8217;s official <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/base-types\/regular-expressions\" target=\"_blank\" rel=\"noreferrer noopener\">.NET Regular Expressions documentation<\/a>, which contains detailed explanations and additional examples.<\/p>\n\n\n\n<p>Now go forth and conquer string processing with C# regular expressions!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Additional Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/base-types\/best-practices\" target=\"_blank\" rel=\"noreferrer noopener\">Regular Expression Performance Best Practices<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/base-types\/regular-expression-language-quick-reference\" target=\"_blank\" rel=\"noreferrer noopener\">Microsoft&#8217;s Regular Expression Language &#8211; Quick Reference<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.text.regularexpressions.regexcompilationinfo\" target=\"_blank\" rel=\"noreferrer noopener\">.NET Regular Expression Design Time Compilation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Regular expressions in C# offer a powerful way to match, search, and manipulate text using pattern-based logic. This tutorial introduces the Regex class from the System.Text.RegularExpressions namespace, demonstrating how to validate input, extract data, and perform replacements efficiently. With practical examples and clear explanations, it\u2019s an ideal starting point for developers looking to harness regex capabilities in their C# applications.\u200b<\/p>\n","protected":false},"author":1,"featured_media":58423,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[4,3481],"class_list":{"0":"post-217","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-programming","8":"tag-c-sharp","9":"tag-regex","10":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>C# Regular Expression: The Ultimate Beginner&#039;s Guide - CodeSamplez.com<\/title>\n<meta name=\"description\" content=\"Learn everything about C# Regular Expression in this comprehensive guide. From basic syntax to advanced techniques, perfect for beginners.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# Regular Expression: The Ultimate Beginner&#039;s Guide\" \/>\n<meta property=\"og:description\" content=\"Learn everything about C# Regular Expression in this comprehensive guide. From basic syntax to advanced techniques, perfect for beginners.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp\" \/>\n<meta property=\"og:site_name\" content=\"CodeSamplez.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codesamplez\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/ranacseruet\" \/>\n<meta property=\"article:published_time\" content=\"2011-01-05T08:42:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-02T15:50:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codesamplez.com\/wp-content\/uploads\/2011\/01\/regular-expression-c-sharp.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1536\" \/>\n\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Rana Ahsan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ranacseruet\" \/>\n<meta name=\"twitter:site\" content=\"@codesamplez\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rana Ahsan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/regular-expression-c-sharp#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/regular-expression-c-sharp\"},\"author\":{\"name\":\"Rana Ahsan\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#\\\/schema\\\/person\\\/a82c3c07205f4bb73d6b3b0906bc328b\"},\"headline\":\"C# Regular Expression: The Ultimate Beginner&#8217;s Guide\",\"datePublished\":\"2011-01-05T08:42:42+00:00\",\"dateModified\":\"2025-05-02T15:50:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/regular-expression-c-sharp\"},\"wordCount\":851,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/regular-expression-c-sharp#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codesamplez.com\\\/wp-content\\\/uploads\\\/2011\\\/01\\\/regular-expression-c-sharp.webp\",\"keywords\":[\"c#\",\"regex\"],\"articleSection\":[\"Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/codesamplez.com\\\/programming\\\/regular-expression-c-sharp#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/regular-expression-c-sharp\",\"url\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/regular-expression-c-sharp\",\"name\":\"C# Regular Expression: The Ultimate Beginner's Guide - CodeSamplez.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/regular-expression-c-sharp#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/regular-expression-c-sharp#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codesamplez.com\\\/wp-content\\\/uploads\\\/2011\\\/01\\\/regular-expression-c-sharp.webp\",\"datePublished\":\"2011-01-05T08:42:42+00:00\",\"dateModified\":\"2025-05-02T15:50:39+00:00\",\"description\":\"Learn everything about C# Regular Expression in this comprehensive guide. From basic syntax to advanced techniques, perfect for beginners.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/regular-expression-c-sharp#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/codesamplez.com\\\/programming\\\/regular-expression-c-sharp\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/regular-expression-c-sharp#primaryimage\",\"url\":\"https:\\\/\\\/codesamplez.com\\\/wp-content\\\/uploads\\\/2011\\\/01\\\/regular-expression-c-sharp.webp\",\"contentUrl\":\"https:\\\/\\\/codesamplez.com\\\/wp-content\\\/uploads\\\/2011\\\/01\\\/regular-expression-c-sharp.webp\",\"width\":1536,\"height\":1024,\"caption\":\"C# Regular Expression Guide\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/regular-expression-c-sharp#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/codesamplez.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# Regular Expression: The Ultimate Beginner&#8217;s Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#website\",\"url\":\"https:\\\/\\\/codesamplez.com\\\/\",\"name\":\"CODESAMPLEZ.COM\",\"description\":\"Programming And Development Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/codesamplez.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#organization\",\"name\":\"codesamplez.com\",\"url\":\"https:\\\/\\\/codesamplez.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/codesamplez.com\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/cropped-favicon.webp\",\"contentUrl\":\"https:\\\/\\\/codesamplez.com\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/cropped-favicon.webp\",\"width\":512,\"height\":512,\"caption\":\"codesamplez.com\"},\"image\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/codesamplez\",\"https:\\\/\\\/x.com\\\/codesamplez\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#\\\/schema\\\/person\\\/a82c3c07205f4bb73d6b3b0906bc328b\",\"name\":\"Rana Ahsan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c7a4f88bcf4a55cd1483386318ebecf27359154275a0b355b0ea186676f9f7f?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c7a4f88bcf4a55cd1483386318ebecf27359154275a0b355b0ea186676f9f7f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c7a4f88bcf4a55cd1483386318ebecf27359154275a0b355b0ea186676f9f7f?s=96&d=mm&r=g\",\"caption\":\"Rana Ahsan\"},\"description\":\"Rana Ahsan is a seasoned software engineer and technology leader specialized in distributed systems and software architecture. With a Master\u2019s in Software Engineering from Concordia University, his experience spans leading scalable architecture at Coursera and TopHat, contributing to open-source projects. This blog, CodeSamplez.com, showcases his passion for sharing practical insights on programming and distributed systems concepts and help educate others. Github | X | LinkedIn\",\"sameAs\":[\"https:\\\/\\\/github.com\\\/ranacseruet\",\"https:\\\/\\\/www.facebook.com\\\/ranacseruet\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/ranacseruet\\\/\",\"https:\\\/\\\/x.com\\\/ranacseruet\"],\"url\":\"https:\\\/\\\/codesamplez.com\\\/author\\\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"C# Regular Expression: The Ultimate Beginner's Guide - CodeSamplez.com","description":"Learn everything about C# Regular Expression in this comprehensive guide. From basic syntax to advanced techniques, perfect for beginners.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp","og_locale":"en_US","og_type":"article","og_title":"C# Regular Expression: The Ultimate Beginner's Guide","og_description":"Learn everything about C# Regular Expression in this comprehensive guide. From basic syntax to advanced techniques, perfect for beginners.","og_url":"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp","og_site_name":"CodeSamplez.com","article_publisher":"https:\/\/www.facebook.com\/codesamplez","article_author":"https:\/\/www.facebook.com\/ranacseruet","article_published_time":"2011-01-05T08:42:42+00:00","article_modified_time":"2025-05-02T15:50:39+00:00","og_image":[{"width":1536,"height":1024,"url":"https:\/\/codesamplez.com\/wp-content\/uploads\/2011\/01\/regular-expression-c-sharp.webp","type":"image\/webp"}],"author":"Rana Ahsan","twitter_card":"summary_large_image","twitter_creator":"@ranacseruet","twitter_site":"@codesamplez","twitter_misc":{"Written by":"Rana Ahsan","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp#article","isPartOf":{"@id":"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp"},"author":{"name":"Rana Ahsan","@id":"https:\/\/codesamplez.com\/#\/schema\/person\/a82c3c07205f4bb73d6b3b0906bc328b"},"headline":"C# Regular Expression: The Ultimate Beginner&#8217;s Guide","datePublished":"2011-01-05T08:42:42+00:00","dateModified":"2025-05-02T15:50:39+00:00","mainEntityOfPage":{"@id":"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp"},"wordCount":851,"commentCount":0,"publisher":{"@id":"https:\/\/codesamplez.com\/#organization"},"image":{"@id":"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp#primaryimage"},"thumbnailUrl":"https:\/\/codesamplez.com\/wp-content\/uploads\/2011\/01\/regular-expression-c-sharp.webp","keywords":["c#","regex"],"articleSection":["Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp","url":"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp","name":"C# Regular Expression: The Ultimate Beginner's Guide - CodeSamplez.com","isPartOf":{"@id":"https:\/\/codesamplez.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp#primaryimage"},"image":{"@id":"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp#primaryimage"},"thumbnailUrl":"https:\/\/codesamplez.com\/wp-content\/uploads\/2011\/01\/regular-expression-c-sharp.webp","datePublished":"2011-01-05T08:42:42+00:00","dateModified":"2025-05-02T15:50:39+00:00","description":"Learn everything about C# Regular Expression in this comprehensive guide. From basic syntax to advanced techniques, perfect for beginners.","breadcrumb":{"@id":"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp#primaryimage","url":"https:\/\/codesamplez.com\/wp-content\/uploads\/2011\/01\/regular-expression-c-sharp.webp","contentUrl":"https:\/\/codesamplez.com\/wp-content\/uploads\/2011\/01\/regular-expression-c-sharp.webp","width":1536,"height":1024,"caption":"C# Regular Expression Guide"},{"@type":"BreadcrumbList","@id":"https:\/\/codesamplez.com\/programming\/regular-expression-c-sharp#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codesamplez.com\/"},{"@type":"ListItem","position":2,"name":"C# Regular Expression: The Ultimate Beginner&#8217;s Guide"}]},{"@type":"WebSite","@id":"https:\/\/codesamplez.com\/#website","url":"https:\/\/codesamplez.com\/","name":"CODESAMPLEZ.COM","description":"Programming And Development Resources","publisher":{"@id":"https:\/\/codesamplez.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codesamplez.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codesamplez.com\/#organization","name":"codesamplez.com","url":"https:\/\/codesamplez.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codesamplez.com\/#\/schema\/logo\/image\/","url":"https:\/\/codesamplez.com\/wp-content\/uploads\/2024\/10\/cropped-favicon.webp","contentUrl":"https:\/\/codesamplez.com\/wp-content\/uploads\/2024\/10\/cropped-favicon.webp","width":512,"height":512,"caption":"codesamplez.com"},"image":{"@id":"https:\/\/codesamplez.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codesamplez","https:\/\/x.com\/codesamplez"]},{"@type":"Person","@id":"https:\/\/codesamplez.com\/#\/schema\/person\/a82c3c07205f4bb73d6b3b0906bc328b","name":"Rana Ahsan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c7a4f88bcf4a55cd1483386318ebecf27359154275a0b355b0ea186676f9f7f?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c7a4f88bcf4a55cd1483386318ebecf27359154275a0b355b0ea186676f9f7f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c7a4f88bcf4a55cd1483386318ebecf27359154275a0b355b0ea186676f9f7f?s=96&d=mm&r=g","caption":"Rana Ahsan"},"description":"Rana Ahsan is a seasoned software engineer and technology leader specialized in distributed systems and software architecture. With a Master\u2019s in Software Engineering from Concordia University, his experience spans leading scalable architecture at Coursera and TopHat, contributing to open-source projects. This blog, CodeSamplez.com, showcases his passion for sharing practical insights on programming and distributed systems concepts and help educate others. Github | X | LinkedIn","sameAs":["https:\/\/github.com\/ranacseruet","https:\/\/www.facebook.com\/ranacseruet","https:\/\/www.linkedin.com\/in\/ranacseruet\/","https:\/\/x.com\/ranacseruet"],"url":"https:\/\/codesamplez.com\/author\/admin"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/codesamplez.com\/wp-content\/uploads\/2011\/01\/regular-expression-c-sharp.webp","jetpack_shortlink":"https:\/\/wp.me\/p1hHlI-3v","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":241,"url":"https:\/\/codesamplez.com\/programming\/regular-expression-basics","url_meta":{"origin":217,"position":0},"title":"Regular Expression Basics For Beginners","author":"Rana Ahsan","date":"January 3, 2011","format":false,"excerpt":"Regular expressions, or regex, are powerful tools for pattern matching in text. This beginner-friendly tutorial breaks down the core concepts, syntax, and practical uses across programming languages. Whether you're validating emails or scraping data, this guide will help you master the basics and start using regex like a pro.","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/codesamplez.com\/category\/programming"},"img":{"alt_text":"regular expression basics","src":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/regular-expression-basics.webp?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/regular-expression-basics.webp?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/regular-expression-basics.webp?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/regular-expression-basics.webp?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/regular-expression-basics.webp?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/regular-expression-basics.webp?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":256,"url":"https:\/\/codesamplez.com\/programming\/regular-expressions-in-php","url_meta":{"origin":217,"position":1},"title":"PHP Regular Expression: Ultimate Beginner&#8217;s Guide","author":"Rana Ahsan","date":"January 9, 2011","format":false,"excerpt":"Have you ever struggled with validating email addresses or extracting specific patterns from strings in your PHP applications? I've been there too. After years of coding, I can absolutely say that PHP Regular Expressions are the most powerful tools in your programming arsenal for text manipulation and pattern matching.","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/codesamplez.com\/category\/programming"},"img":{"alt_text":"PHP Regular Expression Tutorial","src":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/php-regular-expression.webp?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/php-regular-expression.webp?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/php-regular-expression.webp?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/php-regular-expression.webp?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/php-regular-expression.webp?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/php-regular-expression.webp?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":23370,"url":"https:\/\/codesamplez.com\/programming\/c-sharp-lambda-expression","url_meta":{"origin":217,"position":2},"title":"C# Lambda Expressions: The Ultimate Guide","author":"Rana Ahsan","date":"April 7, 2013","format":false,"excerpt":"This tutorial introduces C# lambda expressions within LINQ, demonstrating how to write concise, readable code for querying and manipulating data. Through practical examples, it guides you in integrating lambda expressions into your .NET applications, enhancing code efficiency and clarity.","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/codesamplez.com\/category\/programming"},"img":{"alt_text":"C# Lambda Expression Tutorial","src":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/04\/c-sharp-lambda-expression.webp?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/04\/c-sharp-lambda-expression.webp?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/04\/c-sharp-lambda-expression.webp?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/04\/c-sharp-lambda-expression.webp?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/04\/c-sharp-lambda-expression.webp?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/04\/c-sharp-lambda-expression.webp?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":57385,"url":"https:\/\/codesamplez.com\/front-end\/javascript-test-framework-from-scratch","url_meta":{"origin":217,"position":3},"title":"JavaScript Testing Framework from Scratch: A Complete Guide","author":"Rana Ahsan","date":"February 4, 2025","format":false,"excerpt":"Learn how to build a lightweight JavaScript test framework from scratch without external libraries. This step-by-step guide covers creating expect, test, and describe functions, running tests in the browser, and visualizing results with pass\/fail indicators. Perfect for developers eager to understand testing tools and simplify their workflow! \ud83d\ude80","rel":"","context":"In &quot;Front End&quot;","block_context":{"text":"Front End","link":"https:\/\/codesamplez.com\/category\/front-end"},"img":{"alt_text":"minimalistic javascript framework","src":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/02\/minimalistic-javascript-framework.webp?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/02\/minimalistic-javascript-framework.webp?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/02\/minimalistic-javascript-framework.webp?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/02\/minimalistic-javascript-framework.webp?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":22856,"url":"https:\/\/codesamplez.com\/programming\/c-sharp-dictionary-tutorial","url_meta":{"origin":217,"position":4},"title":"C# Dictionary: Complete Guide to Mastering Key-Value Collections","author":"Rana Ahsan","date":"February 12, 2013","format":false,"excerpt":"This tutorial introduces the C# Dictionary collection, a powerful key-value data structure built on a hash table. It covers initializing dictionaries with default values, adding and removing entries, iterating using foreach with KeyValuePair, and merging dictionaries using LINQ. Additionally, it addresses XML serialization challenges and solutions for dictionaries. Ideal for\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/codesamplez.com\/category\/programming"},"img":{"alt_text":"C# Dictionary","src":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/02\/c-sharp-dictionary.webp?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/02\/c-sharp-dictionary.webp?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/02\/c-sharp-dictionary.webp?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/02\/c-sharp-dictionary.webp?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/02\/c-sharp-dictionary.webp?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/02\/c-sharp-dictionary.webp?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":351,"url":"https:\/\/codesamplez.com\/database\/linq-to-sql-c-sharp-tutorial","url_meta":{"origin":217,"position":5},"title":"LINQ to SQL in C#: The Ultimate Beginner&#8217;s Guide","author":"Rana Ahsan","date":"January 25, 2011","format":false,"excerpt":"This beginner-friendly tutorial introduces LINQ to SQL in C#, guiding you through setting up LINQ to SQL classes, querying data using both SQL-like syntax and lambda expressions, and retrieving single or multiple records. With practical code examples, it helps you integrate LINQ into your .NET applications efficiently.","rel":"","context":"In &quot;Database&quot;","block_context":{"text":"Database","link":"https:\/\/codesamplez.com\/category\/database"},"img":{"alt_text":"LinQ To SQL For Beginners","src":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/linq-to-sql.webp?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/linq-to-sql.webp?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/linq-to-sql.webp?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/linq-to-sql.webp?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/linq-to-sql.webp?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2011\/01\/linq-to-sql.webp?resize=1400%2C800&ssl=1 4x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/posts\/217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/comments?post=217"}],"version-history":[{"count":4,"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/posts\/217\/revisions"}],"predecessor-version":[{"id":58522,"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/posts\/217\/revisions\/58522"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/media\/58423"}],"wp:attachment":[{"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/media?parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/categories?post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/tags?post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}