{"id":1921,"date":"2023-06-01T14:35:36","date_gmt":"2023-06-01T07:35:36","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=1921"},"modified":"2023-06-01T14:36:17","modified_gmt":"2023-06-01T07:36:17","slug":"c-regex-groups","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-regular-expression\/c-regex-groups\/","title":{"rendered":"C# Regex Groups"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about capturing groups and how to use the C# Regex Groups property to get the matches.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the C# Regex Groups<\/h2>\n\n\n\n<p>Suppose you want to match a route that starts with <code>posts<\/code> followed by a forward slash <code>\/<\/code> and an <code>id<\/code> like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">posts\/id<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To do that, you may come up with the following regular expression pattern:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">posts\/\\d+<\/code><\/span><\/pre>\n\n\n<p>And the following program illustrates how you would use that pattern to match the route:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> System.Text.RegularExpressions;\n<span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">static<\/span> System.Console;\n\n\n<span class=\"hljs-keyword\">var<\/span> route = <span class=\"hljs-string\">\"posts\/100\"<\/span>;\n<span class=\"hljs-keyword\">var<\/span> pattern = <span class=\"hljs-string\">@\"posts\/\\d+\"<\/span>;\n\n<span class=\"hljs-keyword\">var<\/span> match = Regex.Match(route, pattern);\n\nWriteLine(match.Value);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">posts\/<span class=\"hljs-number\">100<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The pattern <code>posts\/\\d+<\/code> matches the <code>posts\/<\/code> characters and is followed by one or more digits. It returns the result as expected.<\/p>\n\n\n\n<p>Now, you want to extract the id from the route, which is 100 in this case. To do that, you can use something called <strong>capturing group<\/strong> in regular expressions. <\/p>\n\n\n\n<p>By definition, a capturing group allows you to capture a portion of the matched text. To create a capturing group, you define a subpattern within the overall pattern and closed it within parentheses.<\/p>\n\n\n\n<p>In our example, we can create a subpattern like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">posts\/(\\d+)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This <code>(\\d+)<\/code> is a capturing group and <code>\\d+<\/code> is a subpattern. <\/p>\n\n\n\n<p>If the matches are found, you can access the groups via the <code>Groups<\/code> property of the <code>Match<\/code> object. The type of <code>Groups<\/code> is the <code>GroupCollection<\/code> type which is a collection of <code>Group<\/code> objects.<\/p>\n\n\n\n<p>The first element in the <code>Groups<\/code> collection is the whole match which is <code>posts\/100<\/code> in this example and the next elements are the matched text of the capturing groups.<\/p>\n\n\n\n<p>The following example uses the capturing group to exact the <code>id<\/code> from the route <code>posts\/100<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> System.Text.RegularExpressions;\n<span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">static<\/span> System.Console;\n\n<span class=\"hljs-keyword\">var<\/span> route = <span class=\"hljs-string\">\"posts\/100\"<\/span>;\n<span class=\"hljs-keyword\">var<\/span> pattern = <span class=\"hljs-string\">@\"posts\/(\\d+)\"<\/span>;\n\n<span class=\"hljs-keyword\">var<\/span> match = Regex.Match(route, pattern);\n\n<span class=\"hljs-keyword\">if<\/span> (match.Success)\n{\n    <span class=\"hljs-keyword\">foreach<\/span> (<span class=\"hljs-keyword\">var<\/span> <span class=\"hljs-keyword\">group<\/span> <span class=\"hljs-keyword\">in<\/span> match.Groups)\n    {\n        WriteLine(<span class=\"hljs-keyword\">group<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">posts\/<span class=\"hljs-number\">100<\/span>\n<span class=\"hljs-number\">100<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>A regular expression may contain multiple capturing groups. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> System.Text.RegularExpressions;\n<span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">static<\/span> System.Console;\n\n\n<span class=\"hljs-comment\">\/\/ posts\/year\/month\/date format<\/span>\n<span class=\"hljs-keyword\">var<\/span> route = <span class=\"hljs-string\">\"posts\/2023\/12\/31\"<\/span>;\n\n<span class=\"hljs-keyword\">var<\/span> pattern = <span class=\"hljs-string\">@\"posts\/(\\d{4})\/(\\d{2})\/(\\d{2})\"<\/span>;\n\n<span class=\"hljs-keyword\">var<\/span> match = Regex.Match(route, pattern);\n\n<span class=\"hljs-keyword\">if<\/span> (match.Success)\n{\n    <span class=\"hljs-keyword\">foreach<\/span> (<span class=\"hljs-keyword\">var<\/span> <span class=\"hljs-keyword\">group<\/span> <span class=\"hljs-keyword\">in<\/span> match.Groups)\n    {\n        WriteLine(<span class=\"hljs-keyword\">group<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">posts\/<span class=\"hljs-number\">2023<\/span>\/<span class=\"hljs-number\">12<\/span>\/<span class=\"hljs-number\">31<\/span>\n<span class=\"hljs-number\">2023<\/span>\n<span class=\"hljs-number\">12<\/span>\n<span class=\"hljs-number\">31<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we have three capturing groups for year, month, and date in the route. The output shows four groups, which include the whole match.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Named capturing groups<\/h2>\n\n\n\n<p>By using the <code>Groups<\/code> property, you can iterate the groups or access a group by index. For example, the  <code>Groups[1]<\/code> should return the year which is <code>2023<\/code>:<\/p>\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\">using System;\r\nusing System.Text.RegularExpressions;\r\nusing <span class=\"hljs-keyword\">static<\/span> System.Console;\r\n\r\n\r\n<span class=\"hljs-comment\">\/\/ posts\/year\/month\/date format<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> route = <span class=\"hljs-string\">\"posts\/2023\/12\/31\"<\/span>;\r\n\r\n<span class=\"hljs-keyword\">var<\/span> pattern = @<span class=\"hljs-string\">\"posts\/(\\d{4})\/(\\d{2})\/(\\d{2})\"<\/span>;\r\n\r\n<span class=\"hljs-keyword\">var<\/span> match = Regex.Match(route, pattern);\r\n\r\n<span class=\"hljs-keyword\">if<\/span> (match.Success &amp;&amp; match.Groups.Count == <span class=\"hljs-number\">4<\/span>)\r\n{\r\n    <span class=\"hljs-keyword\">var<\/span> year = match.Groups&#91;<span class=\"hljs-number\">1<\/span>];\r\n    <span class=\"hljs-keyword\">var<\/span> month = match.Groups&#91;<span class=\"hljs-number\">2<\/span>];\r\n    <span class=\"hljs-keyword\">var<\/span> day = match.Groups&#91;<span class=\"hljs-number\">3<\/span>];\r\n    WriteLine($<span class=\"hljs-string\">\"{year}\/{month}\/{day}\"<\/span>);\r\n}<\/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<p>It&#8217;s more explicit to assign a name to each capturing in the regular expression so that we can access each group by name rather than by index.<\/p>\n\n\n\n<p>To do that, you can use the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">(?&lt;name&gt;rule)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>()<\/code> denotes a capturing group.<\/li>\n\n\n\n<li>The <code>?&lt;name><\/code> specifies the capturing group&#8217;s name.<\/li>\n\n\n\n<li>The <code>rule<\/code> is a subpattern.<\/li>\n<\/ul>\n\n\n\n<p>To access the matched value, you look it up in the <code>Groups<\/code> property by group name like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">Groups&#91;<span class=\"hljs-string\">\"name\"<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> System.Text.RegularExpressions;\n<span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">static<\/span> System.Console;\n\n\n<span class=\"hljs-comment\">\/\/ posts\/year\/month\/date format<\/span>\n<span class=\"hljs-keyword\">var<\/span> route = <span class=\"hljs-string\">\"posts\/2023\/12\/31\"<\/span>;\n\n<span class=\"hljs-keyword\">var<\/span> pattern = <span class=\"hljs-string\">@\"posts\/(?&lt;year&gt;\\d{4})\/(?&lt;month&gt;\\d{2})\/(?&lt;day&gt;\\d{2})\"<\/span>;\n\n<span class=\"hljs-keyword\">var<\/span> match = Regex.Match(route, pattern);\n\n<span class=\"hljs-keyword\">if<\/span> (match.Success)\n{\n    <span class=\"hljs-keyword\">var<\/span> year = match.Groups&#91;<span class=\"hljs-string\">\"year\"<\/span>];\n    <span class=\"hljs-keyword\">var<\/span> month = match.Groups&#91;<span class=\"hljs-string\">\"month\"<\/span>];\n    <span class=\"hljs-keyword\">var<\/span> day = match.Groups&#91;<span class=\"hljs-string\">\"day\"<\/span>];\n    WriteLine(<span class=\"hljs-string\">$\"<span class=\"hljs-subst\">{year}<\/span>\/<span class=\"hljs-subst\">{month}<\/span>\/<span class=\"hljs-subst\">{day}<\/span>\"<\/span>);\n\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-number\">2023<\/span>\/<span class=\"hljs-number\">12<\/span>\/<span class=\"hljs-number\">31<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Place a pattern inside the parentheses <code>()<\/code> to create a capturing group.<\/li>\n\n\n\n<li>Use the <code>Match.Groups<\/code> property to access the groups. The first group is the whole match and the next ones are the matched groups.<\/li>\n\n\n\n<li>Use the <code>(?&lt;name>rule)<\/code> syntax to assign a <code>name<\/code> to a capturing group and access its matched value using <code>Groups[\"name\"]<\/code>.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1921\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-regular-expression\/c-regex-groups\/\"\n\t\t\t\tdata-post-title=\"C# Regex Groups\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1921\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-regular-expression\/c-regex-groups\/\"\n\t\t\t\tdata-post-title=\"C# Regex Groups\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn about capturing groups and how to use the C# Regex Groups property to get the matches. Introduction to the C# Regex Groups Suppose you want to match a route that starts with posts followed by a forward slash \/ and an id like this: To do that, you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1892,"menu_order":8,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1921","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=1921"}],"version-history":[{"count":2,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1921\/revisions"}],"predecessor-version":[{"id":1923,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1921\/revisions\/1923"}],"up":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1892"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=1921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}