{"id":12464,"date":"2024-03-14T15:55:42","date_gmt":"2024-03-14T15:55:42","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/"},"modified":"2025-08-05T01:52:37","modified_gmt":"2025-08-05T01:52:37","slug":"how-to-use-regex-to-match-multiple-strings-in-c","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/","title":{"rendered":"C# Regex: Match Multiple Strings Explained"},"content":{"rendered":"<p>In C#, you can use the Regex class to match multiple strings. Here is an example code:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">using<\/span> System;\r\n<span class=\"hljs-keyword\">using<\/span> System.Text.RegularExpressions;\r\n\r\n<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Program<\/span>\r\n{\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Main<\/span>()<\/span>\r\n    {\r\n        <span class=\"hljs-built_in\">string<\/span> input = <span class=\"hljs-string\">\"Hello world! This is a test.\"<\/span>;\r\n        <span class=\"hljs-built_in\">string<\/span>[] keywords = { <span class=\"hljs-string\">\"Hello\"<\/span>, <span class=\"hljs-string\">\"world\"<\/span>, <span class=\"hljs-string\">\"test\"<\/span> };\r\n\r\n        <span class=\"hljs-keyword\">foreach<\/span> (<span class=\"hljs-built_in\">string<\/span> keyword <span class=\"hljs-keyword\">in<\/span> keywords)\r\n        {\r\n            <span class=\"hljs-comment\">\/\/ \u4f7f\u7528Regex\u7c7b\u7684\u9759\u6001\u65b9\u6cd5Match\u6765\u8fdb\u884c\u5339\u914d<\/span>\r\n            Match match = Regex.Match(input, keyword);\r\n\r\n            <span class=\"hljs-keyword\">if<\/span> (match.Success)\r\n            {\r\n                Console.WriteLine(<span class=\"hljs-string\">\"Found '{0}' at position {1}.\"<\/span>, keyword, match.Index);\r\n            }\r\n            <span class=\"hljs-keyword\">else<\/span>\r\n            {\r\n                Console.WriteLine(<span class=\"hljs-string\">\"'{0}' not found.\"<\/span>, keyword);\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>The output will be:<\/p>\n<pre class=\"post-pre\"><code>Found 'Hello' at position 0.\r\nFound 'world' at position 6.\r\nFound 'test' at position 21.\r\n<\/code><\/pre>\n<p>In the example above, we defined a string array called keywords and used a loop along with the Regex.Match method to search for each keyword in the input string. If a match is found, it will output the position of the keyword; otherwise, it will output &#8220;not found&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In C#, you can use the Regex class to match multiple strings. Here is an example code: using System; using System.Text.RegularExpressions; class Program { static void Main() { string input = &#8220;Hello world! This is a test.&#8221;; string[] keywords = { &#8220;Hello&#8221;, &#8220;world&#8221;, &#8220;test&#8221; }; foreach (string keyword in keywords) { \/\/ \u4f7f\u7528Regex\u7c7b\u7684\u9759\u6001\u65b9\u6cd5Match\u6765\u8fdb\u884c\u5339\u914d Match match [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[3607,274,381,1803,6542],"class_list":["post-12464","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-regex","tag-c","tag-c-programming","tag-regular-expressions","tag-string-matching"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C# Regex: Match Multiple Strings Explained - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn to match multiple keywords in C# using Regex class with efficient code examples and best practices.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# Regex: Match Multiple Strings Explained\" \/>\n<meta property=\"og:description\" content=\"Learn to match multiple keywords in C# using Regex class with efficient code examples and best practices.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-14T15:55:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-05T01:52:37+00:00\" \/>\n<meta name=\"author\" content=\"William Carter\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"William Carter\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/\"},\"author\":{\"name\":\"William Carter\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0\"},\"headline\":\"C# Regex: Match Multiple Strings Explained\",\"datePublished\":\"2024-03-14T15:55:42+00:00\",\"dateModified\":\"2025-08-05T01:52:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/\"},\"wordCount\":76,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"$regex\",\"c#\",\"C++ Programming\",\"regular expressions\",\"string matching\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/\",\"name\":\"C# Regex: Match Multiple Strings Explained - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-14T15:55:42+00:00\",\"dateModified\":\"2025-08-05T01:52:37+00:00\",\"description\":\"Learn to match multiple keywords in C# using Regex class with efficient code examples and best practices.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# Regex: Match Multiple Strings Explained\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0\",\"name\":\"William Carter\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g\",\"caption\":\"William Carter\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/williamcarter\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"C# Regex: Match Multiple Strings Explained - Blog - Silicon Cloud","description":"Learn to match multiple keywords in C# using Regex class with efficient code examples and best practices.","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:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/","og_locale":"en_US","og_type":"article","og_title":"C# Regex: Match Multiple Strings Explained","og_description":"Learn to match multiple keywords in C# using Regex class with efficient code examples and best practices.","og_url":"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-14T15:55:42+00:00","article_modified_time":"2025-08-05T01:52:37+00:00","author":"William Carter","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"William Carter","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/"},"author":{"name":"William Carter","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0"},"headline":"C# Regex: Match Multiple Strings Explained","datePublished":"2024-03-14T15:55:42+00:00","dateModified":"2025-08-05T01:52:37+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/"},"wordCount":76,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["$regex","c#","C++ Programming","regular expressions","string matching"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/","url":"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/","name":"C# Regex: Match Multiple Strings Explained - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-14T15:55:42+00:00","dateModified":"2025-08-05T01:52:37+00:00","description":"Learn to match multiple keywords in C# using Regex class with efficient code examples and best practices.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-regex-to-match-multiple-strings-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"C# Regex: Match Multiple Strings Explained"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0","name":"William Carter","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g","caption":"William Carter"},"url":"https:\/\/www.silicloud.com\/blog\/author\/williamcarter\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/12464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=12464"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/12464\/revisions"}],"predecessor-version":[{"id":156256,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/12464\/revisions\/156256"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=12464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=12464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=12464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}