{"id":507,"date":"2021-08-27T13:59:36","date_gmt":"2021-08-27T04:59:36","guid":{"rendered":"https:\/\/mvc.auctionpro.co.kr\/?page_id=507"},"modified":"2025-05-20T17:55:25","modified_gmt":"2025-05-20T08:55:25","slug":"c-regex-%ec%98%88%ec%a0%9c","status":"publish","type":"page","link":"https:\/\/mvc.auctionpro.co.kr\/?page_id=507","title":{"rendered":"C# Regex \uc608\uc81c"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1.  \uc788\ub098 \uc5c6\ub098 \ud655\uc778<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \">using System;\nusing System.Text.RegularExpressions;\n\nnamespace Console\n{\n    class Program\n    {          \n        static async Task Main(string[] args)\n        {\n\nvar words = new List&lt;string&gt;() { \"Seven\", \"even\", \"Maven\", \"Amen\", \"eleven\" };\n\n            var rx = new Regex(@\".even\", RegexOptions.Compiled);\n\n            foreach (string word in words)\n            {\n                if (rx.IsMatch(word))\n                {\n                    Console.WriteLine($\"{word} does match\");\n                }\n                else\n                {\n                    Console.WriteLine($\"{word} does not match\");\n                }\n            }\n\n           Console.ReadLine();\n         }\n     }\n}<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"theme:vs2012-black lang:default decode:true \" title=\"Result\">Seven does match\neven does not match\nMaven does not match\nAmen does not match\neleven does match<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. \ucc3e\ub294 \ubb38\uc7a5  \uc704\uce58 \ucc3e\uae30 <\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \">            var content = @\"Foxes are omnivorous mammals belonging to several genera\n                            of the family Canidae. Foxes have a flattened skull, upright triangular ears,\n                            a pointed, slightly upturned snout, and a long bushy tail. Foxes live on every\n                            continent except Antarctica. By far the most common and widespread species of\n                           fox is the red fox.\";\n\n            var rx = new Regex(\"fox(es)?\", RegexOptions.Compiled |\n                RegexOptions.IgnoreCase);\n\n            Match match = rx.Match(content);\n            \n            \/\/Console.WriteLine(match.Value);\n\n            while (match.Success)\n            {\n                Console.WriteLine($\"{match.Value} at index {match.Index}\");\n                match = match.NextMatch();\n            }\n            Console.ReadLine();\n        }<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \" title=\"Result\">Foxes at index 0\nFoxes at index 82\nFoxes at index 198\nfox at index 300\nfox at index 315<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3. \ub2e8\uc5b4\ubcc4\ub85c is \ucc3e\uae30 \/ Boundary\ub85c  is \ucc3e\uae30 \/ \\b&nbsp;assert position at a word boundary:&nbsp;(^\\w|\\w$|\\W\\w|\\w\\W)<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \"> var text = \"This island is beautiful\";\n\n            var rx = new Regex(@\"\\bis\\b\", RegexOptions.Compiled);\n            var matches = rx.Matches(text);\n\n            \/\/Console.WriteLine(match.Value);\n\n            foreach (Match match in matches)\n            {\n                Console.WriteLine($\"{match.Value} at {match.Index}\");\n            }<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"theme:vs2012-black lang:default decode:true \">is at 12<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">4. \ub2e8\uc5b4\ubcc4\ub85c \ubaa8\ub450 \ub098\ub204\uae30<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \">            var content = @\"1Foxes 2 ; are omnivorous mammals belonging to several genera \n                            of the family Canidae. Foxes have a flattened skull, upright triangular ears, \n                            a pointed, slightly upturned snout, and a long bushy tail. Foxes live on every \n                            continent except Antarctica. By far the most common and widespread species of \n                            fox is the red fox.\";\n\n            var rx = new Regex(@\"\\w+\", RegexOptions.Compiled |\n                RegexOptions.IgnoreCase);\n\n            var matches = rx.Matches(content);\n            Console.WriteLine(matches.Count);\n\n            foreach (var match in matches)\n            {\n                Console.WriteLine(match);\n            }<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"theme:vs2012-black lang:default decode:true \" title=\"Result\">52\nFoxes1\n2\nare\nomnivorous\nmammals\nbelonging\nto\nseveral\ngenera\nof\nthe\nfamily\nCanidae.\nFoxes\nhave\na\nflattened\nskull,\nupright\ntriangular\nears,\na\npointed,\nslightly\nupturned\nsnout,\nand\na\nlong\nbushy\ntail.\nFoxes\nlive\non\nevery\ncontinent\nexcept\nAntarctica.\nBy\nfar\nthe\nmost\ncommon\nand\nwidespread\nspecies\nof\nfox\nis\nthe\nred\nfox<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">5. \uccab\ubb38\uc7a5\uc73c\ub85c \uc788\ub098 \ucc3e\uae30<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \">Console.OutputEncoding = System.Text.Encoding.UTF8;\n\n            var sentences = new List&lt;string&gt;() {\n    \"I am looking for Jane.\",\n    \"Jane was walking along the river.\",\n    \"Kate and Jane are close friends.\"\n};\n\n            var rx = new Regex(@\"^Jane\", RegexOptions.Compiled);\n\n            foreach (string sentence in sentences)\n            {\n                if (rx.IsMatch(sentence))\n                {\n                    Console.WriteLine($\"{sentence} does match\");\n                }\n                else\n                {\n                    Console.WriteLine($\"{sentence} does not match\");\n                }\n            }<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"theme:vs2012-black lang:default decode:true \" title=\"Result\">I am looking for Jane.does not match\nJane was walking along the river.does match\nKate and Jane are close friends.does not match<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">6. \uc120\ud0dd\ubb38\uc7a5\ub4e4 \ud558\ub098\ub77c\ub3c4 \uc788\ub098 \ucc3e\uae30<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \">            var users = new List&lt;string&gt;() {\"Jane\", \"Thomas\", \"Robert\",\n    \"Lucy\", \"Beky\", \"John\", \"Peter\", \"Andy\"};\n\n            var rx = new Regex(\"Jane|Beky|Robert\", RegexOptions.Compiled);\n\n            foreach (string user in users)\n            {\n                if (rx.IsMatch(user))\n                {\n                    Console.WriteLine($\"{user} does match\");\n                }\n                else\n                {\n                    Console.WriteLine($\"{user} does not match\");\n                }\n            }<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"theme:vs2012-black lang:default decode:true \" title=\"Result\">Jane does match\nThomas does not match\nRobert does match\nLucy does not match\nBeky does match\nJohn does not match\nPeter does not match\nAndy does not match<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">7. \uc644\uc804 \ubb38\uc7a5 \ucc3e\uae30  \/ capturing&nbsp;  \ud45c\uc2dc : (  )<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \">            var sites = new List&lt;string&gt;() {\"webcode.me\",\n    \"zetcode.com\", \"freebsd.org\", \"netbsd.org\"};\n\n            var rx = new Regex(@\"(\\w+)\\.(\\w+)\", RegexOptions.Compiled);\n\n            foreach (var site in sites)\n            {\n                Match match = rx.Match(site);\n\n                if (match.Success)\n                {\n                    Console.WriteLine(match.Value);\n                    Console.WriteLine(match.Groups[1]);\n                    Console.WriteLine(match.Groups[2]);\n                }\n            }<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"theme:vs2012-black lang:default decode:true \" title=\"Result\">webcode.me\nwebcode\nme\nzetcode.com\nzetcode\ncom\nfreebsd.org\nfreebsd\norg\nnetbsd.org\nnetbsd\norg<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">8. (Group)  \uc0ac\uc6a9\ud558\uae30 <\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \">string[] expressions = { \"16 + 11\", \"12 * 5\", \"27 \/ 3\", \"2 - 8\" };\n            string pattern = @\"(\\d+)\\s+([-+*\/])\\s+(\\d+)\";\n\n            foreach (var expression in expressions)\n            {\n                var rx = new Regex(pattern, RegexOptions.Compiled);\n                var matches = rx.Matches(expression);\n\n                foreach (Match match in matches)\n                {\n                    int val1 = Int32.Parse(match.Groups[1].Value);\n                    int val2 = Int32.Parse(match.Groups[3].Value);\n\n                    var oper = match.Groups[2].Value;\n\n                    switch (oper)\n                    {\n                        case (\"+\"):\n                            Console.WriteLine(\"{0} = {1}\", match.Value, val1 + val2);\n                            break;\n                        case (\"-\"):\n                            Console.WriteLine(\"{0} = {1}\", match.Value, val1 - val2);\n                            break;\n                        case (\"*\"):\n                            Console.WriteLine(\"{0} = {1}\", match.Value, val1 * val2);\n                            break;\n                        case (\"\/\"):\n                            Console.WriteLine(\"{0} = {1}\", match.Value, val1 \/ val2);\n                            break;\n                    }\n                }<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"theme:vs2012-black lang:default decode:true \" title=\"Result\">16 + 11 = 27\n12 * 5 = 60\n27 \/ 3 = 9\n2 - 8 = -6<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">9. (Group) \uc548\uc5d0 \uadf8\ub8f9 (Group)<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \">string text = \"Today is a beautiful day. The sun is shining.\";\n            string pattern = @\"\\b(\\w+\\s*)+\\.\";\n\n            MatchCollection matches = Regex.Matches(text, pattern);\n\n            foreach (Match match in matches)\n            {\n                Console.WriteLine(\"Matched sentence: {0}\", match.Value);\n\n                for (int i = 0; i &lt; match.Groups.Count; i++)\n                {\n                    Console.WriteLine(\"\\tGroup {0}:  {1}\", i, match.Groups[i].Value);\n\n                    int captures = 0;\n\n                    foreach (Capture capture in match.Groups[i].Captures)\n                    {\n                        Console.WriteLine(\"\\t\\tCapture {0}: {1}\", captures, capture.Value);\n                        captures++;\n                    }\n                }\n            }<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"theme:vs2012-black lang:default decode:true \" title=\"Result\">Matched sentence: Today is a beautiful day.\n        Group 0:  Today is a beautiful day.\n                Capture 0: Today is a beautiful day.\n        Group 1:  day\n                Capture 0: Today\n                Capture 1: is\n                Capture 2: a\n                Capture 3: beautiful\n                Capture 4: day\nMatched sentence: The sun is shining.\n        Group 0:  The sun is shining.\n                Capture 0: The sun is shining.\n        Group 1:  shining\n                Capture 0: The\n                Capture 1: sun\n                Capture 2: is\n                Capture 3: shining<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">10. \ub300\uc18c\ubb38\uc790 \uad6c\ubcc4\uc5c6\uc774 \/RegexOpts.IngnoreCase  \ud655\uc778 <\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \">var words = new List&lt;string&gt;() { \"dog\", \"Dog\", \"DOG\", \"Doggy\" };\n\n            var rx = new Regex(@\"\\bdog\\b\", RegexOptions.Compiled |\n                RegexOptions.IgnoreCase);\n\n            foreach (string word in words)\n            {\n                if (rx.IsMatch(word))\n                {\n                    Console.WriteLine($\"{word} does match\");\n                }\n                else\n                {\n                    Console.WriteLine($\"{word} does not match\");\n                }\n            }<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"theme:vs2012-black lang:default decode:true \" title=\"Result\">dog does match\nDog does match\nDOG does match\nDoggy does not match\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">11. \uc7a5\ubb38\uc5d0\uc11c \ub2e8\uc5b4\uc640 \uac1c\uc218 \ucc3e\uae30 LINK Select<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \">var fileName = @\"D:\\bible.txt\";\n            var text = File.ReadAllText(fileName);\n\n            \/\/MatchCollection matches = Regex.Matches(text, @\"[a-z-A-Z']+\");\n            MatchCollection matches = new Regex(\"[a-z-A-Z']+\").Matches(text);\n\n            \/\/List&lt;string&gt; words = matches.Cast&lt;Match&gt;().Select(m =&gt; m.Value).ToList();\n            var words = matches.Cast&lt;Match&gt;().Select(m =&gt; m.Value).ToList();\n\n            var res = words\n                .GroupBy(m =&gt; m)\n                .OrderByDescending(g =&gt; g.Count())\n                .Select(x =&gt; new { word = x.Key, Count = x.Count() })\n                .Take(10);\n\n            foreach (var r in res)\n            {\n                Console.WriteLine($\"{r.word}: {r.Count}\");\n            }<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"theme:vs2012-black lang:default decode:true \" title=\"Result\">the: 62103\nand: 38848\nof: 34478\nto: 13400\nAnd: 12846\nthat: 12576\nin: 12331\nshall: 9760\nhe: 9665\nunto: 8942\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">12.  Regex \ub85c Split \ud558\uae30 <\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \">using System;  \nusing System.Text.RegularExpressions;  \nusing System.Collections.Generic; \n------------------------------------------------\nstring Text = \"1 One, 2 Two, 3 Three is good.\";  \n  \nstring[] digits = Regex.Split(Text, @\"\\D+\");  \n  \nforeach (string value in digits)  \n{  \n    int number;  \n    if (int.TryParse(value, out number))  \n    {  \n        Console.WriteLine(value);  \n    }  \n} <\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">13. \ubb38\uc790\uc5f4 \uc804\uccb4 \uae38\uc774\uac00 20\uc790\ub9ac \uc774\uc0c1\uc778 \uacbd\uc6b0\ub294 \uc81c\uc678 \ucd94\uac00<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:vim decode:true \" >var sRegex = @\"^(?=.{1,19}$)[0-9]{1,2}\\.\\s{1}\";<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. \uc788\ub098 \uc5c6\ub098 \ud655\uc778 2. \ucc3e\ub294 \ubb38\uc7a5 \uc704\uce58 \ucc3e\uae30 3. \ub2e8\uc5b4\ubcc4\ub85c is \ucc3e\uae30 \/ Boundary\ub85c is \ucc3e\uae30 \/ \\b&nbsp;assert position at a word boundary:&nbsp;(^\\w|\\w$|\\W\\w|\\w\\W) 4. \ub2e8\uc5b4\ubcc4\ub85c \ubaa8\ub450 \ub098\ub204\uae30 5. \uccab\ubb38\uc7a5\uc73c\ub85c \uc788\ub098 \ucc3e\uae30 6. \uc120\ud0dd\ubb38\uc7a5\ub4e4 \ud558\ub098\ub77c\ub3c4 \uc788\ub098 \ucc3e\uae30 7. \uc644\uc804 \ubb38\uc7a5 \ucc3e\uae30 \/ capturing&nbsp; \ud45c\uc2dc : ( ) 8. (Group) \uc0ac\uc6a9\ud558\uae30 9. (Group) \uc548\uc5d0 \uadf8\ub8f9 (Group)\u2026 <span class=\"read-more\"><a href=\"https:\/\/mvc.auctionpro.co.kr\/?page_id=507\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-507","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/pages\/507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=507"}],"version-history":[{"count":16,"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/pages\/507\/revisions"}],"predecessor-version":[{"id":872,"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/pages\/507\/revisions\/872"}],"wp:attachment":[{"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}