{"id":1918,"date":"2023-06-01T13:40:28","date_gmt":"2023-06-01T06:40:28","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=1918"},"modified":"2023-06-01T13:40:29","modified_gmt":"2023-06-01T06:40:29","slug":"regex-sets-and-ranges","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-regular-expression\/regex-sets-and-ranges\/","title":{"rendered":"Regex Sets and Ranges"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about regex sets and ranges to create patterns that match characters in a set of characters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sets<\/h2>\n\n\n\n<p>When you place some characters inside square brackets <code>[]<\/code>, you create a set. For example, the <code>[aeiou]<\/code> set matches any single lowercase vowel character.<\/p>\n\n\n\n<p>Typically, a set can be used with regular characters. Also, a set may contain multiple characters, each character within a set corresponds to only one character in the match. <\/p>\n\n\n\n<p>For example, <code>licen[cs]e<\/code> pattern matches both <code>licence<\/code> and <code>license<\/code> as shown in the following example:<\/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\"><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> text = <span class=\"hljs-string\">\"A licence or license\"<\/span>;\n<span class=\"hljs-keyword\">var<\/span> pattern = <span class=\"hljs-string\">@\"licen&#91;cs]e\"<\/span>;\n\n<span class=\"hljs-keyword\">var<\/span> matches = Regex.Matches(text, pattern);\n\n<span class=\"hljs-keyword\">foreach<\/span> (<span class=\"hljs-keyword\">var<\/span> match <span class=\"hljs-keyword\">in<\/span> matches)\n{\n    WriteLine(match);\n}<\/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>Output:<\/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\">licence\nlicense<\/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<h2 class=\"wp-block-heading\">Ranges<\/h2>\n\n\n\n<p>When a set has many characters e.g., from a to z, you don&#8217;t want to list them all in square brackets. Instead, you use a character range (<code>-<\/code>) like this:<\/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\">&#91;<span class=\"hljs-meta\">a-z<\/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 <code>[a-z]<\/code>1 is called a range that matches any character from <code>a<\/code> to <code>z<\/code>. Similarly, you can define a range of numbers from 0 to 9 as follows:<\/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\">&#91;<span class=\"hljs-meta\">0-9<\/span>]<\/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>To define multiple ranges, you can place them next to each other inside the square brackets. For example:<\/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\">&#91;<span class=\"hljs-meta\">a-z0-9<\/span>]<\/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>The range <code>[a-z0-9]<\/code> matches any character that is from <code>a<\/code> to <code>z<\/code> and <code>0<\/code> to <code>9<\/code>.<\/p>\n\n\n\n<p>The following example uses the regex ranges to define a pattern that matches the time string in the format <code>hh:mm<\/code>:<\/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\"><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> text = <span class=\"hljs-string\">\"12:30 15:60\"<\/span>;\n<span class=\"hljs-keyword\">var<\/span> pattern = <span class=\"hljs-string\">@\"\\b&#91;0-2]&#91;0-9]:&#91;0-5]&#91;0-9]\\b\"<\/span>;\n\n<span class=\"hljs-keyword\">var<\/span> matches = Regex.Matches(text, pattern);\n\n<span class=\"hljs-keyword\">foreach<\/span> (<span class=\"hljs-keyword\">var<\/span> match <span class=\"hljs-keyword\">in<\/span> matches)\n{\n    WriteLine(match);\n}<\/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>Output:<\/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-number\">12<\/span>:<span class=\"hljs-number\">30<\/span><\/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>The pattern <\/p>\n\n\n\n<p>This pattern <code>\\b[0-2][0-9]:[0-5][0-9]\\b<\/code> matches the time format <code>hh:mm<\/code>, where the hour can be in the range of 00 to 23 and the minute can be in the range of 00 to 59.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Excluding sets and ranges<\/h2>\n\n\n\n<p>Regular expressions use the caret character <code>^<\/code> to negate a set or a range. For example, the range <code>[^0-9]<\/code> matches any character excluding a digit, which is equivalent to the <code>\\D<\/code> <a href=\"https:\/\/csharptutorial.net\/csharp-regular-expression\/csharp-regex-character-classes\/\">character class<\/a>.<\/p>\n\n\n\n<p>It&#8217;s important to notice that when you use <code>^<\/code> inside a square bracket, it negates a set or a range. But if you use outside of a bracket, it is an anchor character that matches at the beginning of the string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use square brackets <code>[]<\/code> to define a set or a range.<\/li>\n\n\n\n<li>Use the caret <code>^<\/code> inside the square bracket <code>[]<\/code> to negate a set or a range <code>[^...]<\/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=\"1918\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-regular-expression\/regex-sets-and-ranges\/\"\n\t\t\t\tdata-post-title=\"Regex Sets and Ranges\"\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=\"1918\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-regular-expression\/regex-sets-and-ranges\/\"\n\t\t\t\tdata-post-title=\"Regex Sets and Ranges\"\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>In this tutorial, you will learn about regex sets and ranges to create patterns that match characters in a set of characters.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1892,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1918","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1918","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=1918"}],"version-history":[{"count":1,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1918\/revisions"}],"predecessor-version":[{"id":1919,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1918\/revisions\/1919"}],"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=1918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}