{"id":400,"date":"2014-07-28T14:19:01","date_gmt":"2014-07-28T13:19:01","guid":{"rendered":"http:\/\/floris.briolas.nl\/floris\/?p=400"},"modified":"2014-07-29T06:55:08","modified_gmt":"2014-07-29T05:55:08","slug":"string-equals-based-on-regex-c","status":"publish","type":"post","link":"https:\/\/floris.briolas.nl\/floris\/2014\/07\/string-equals-based-on-regex-c\/","title":{"rendered":"String equals based on Regex C#"},"content":{"rendered":"<p>I need to compare strings based on what is matched by a regex. So I decided to create a regex to pick the parts of a string that I felt was necessary for consideration for equals. When using a regex and leaving parts of the string outside use &#8220;non capturing groups&#8221;. \u00a0 Like &#8220;(\\d+)(?:st|nd|rd|th)?&#8221; with will match a number and optional some postfix like &#8220;nd&#8221; as in 2nd.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"c#\" name=\"code\">public static class StringEqualsRegex\r\n{\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ Equals the strings by regex.\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"s1\"&gt;The first string.&lt;\/param&gt;\r\n    \/\/\/ &lt;param name=\"s2\"&gt;The second string.&lt;\/param&gt;\r\n    \/\/\/ &lt;param name=\"regex\"&gt;The regex.&lt;\/param&gt;\r\n    \/\/\/ &lt;returns&gt;&lt;c&gt;true&lt;\/c&gt; if the captured strings of s1 and s2 match, &lt;c&gt;false&lt;\/c&gt; otherwise.&lt;\/returns&gt;\r\n    public static bool EqualsByRegex(this string s1, string s2, Regex regex)\r\n    {   \r\n        \/*Extension Method*\/\r\n        return EqualStringByRegex(s1, s2, regex);\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ Equals the strings by regex.\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"s1\"&gt;The first string.&lt;\/param&gt;\r\n    \/\/\/ &lt;param name=\"s2\"&gt;The second string.&lt;\/param&gt;\r\n    \/\/\/ &lt;param name=\"regex\"&gt;The regex.&lt;\/param&gt;\r\n    \/\/\/ &lt;returns&gt;&lt;c&gt;true&lt;\/c&gt; if the captured strings of s1 and s2 match, &lt;c&gt;false&lt;\/c&gt; otherwise.&lt;\/returns&gt;\r\n    public static bool EqualStringByRegex(string s1, string s2, Regex regex)\r\n    {\r\n\r\n        string capturedS1 = GetCapturedString(s1, regex);\r\n        string capturedS2 = GetCapturedString(s2, regex);\r\n\r\n        return (capturedS1 != null &amp;&amp; capturedS2 != null) &amp;&amp; \r\n            capturedS1.Equals(capturedS2);\r\n    }\r\n\r\n    private static string GetCapturedString(string s, Regex regex)\r\n    {\r\n        dynamic match = regex.Match(s);\r\n\r\n        if ((match.Success))\r\n        {\r\n            var sb = new StringBuilder();\r\n            for (var i = 1; i &lt;= match.Groups.Count; i++)\r\n            {\r\n                foreach (Capture capture in match.Groups[i].Captures)\r\n                {\r\n                    sb.Append(capture.Value);\r\n                }\r\n            }\r\n            return sb.ToString();\r\n        }\r\n\r\n        return null;\r\n    }\r\n\r\n} \r\n<\/pre>\n<p>.<\/p>\n<p>[wpdm_file id=1]<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I need to compare strings based on what is matched by a regex. So I decided to create a regex to pick the parts of a string that I felt was necessary for consideration for equals. When using a regex and leaving parts of the string outside use &#8220;non capturing groups&#8221;. \u00a0 Like &#8220;(\\d+)(?:st|nd|rd|th)?&#8221; with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_newsletter_tier_id":0,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[3],"tags":[7],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p61yPs-6s","_links":{"self":[{"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/posts\/400"}],"collection":[{"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/comments?post=400"}],"version-history":[{"count":17,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/posts\/400\/revisions"}],"predecessor-version":[{"id":419,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/posts\/400\/revisions\/419"}],"wp:attachment":[{"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/media?parent=400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/categories?post=400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/tags?post=400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}