{"id":2353,"date":"2006-08-15T05:40:00","date_gmt":"2006-08-15T05:40:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/regex-match-this-might-save-you-5-minutes-of-head-scratching\/"},"modified":"2016-07-28T10:48:53","modified_gmt":"2016-07-28T10:48:53","slug":"regex-match-this-might-save-you-5-minutes-of-head-scratching","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/regex-match-this-might-save-you-5-minutes-of-head-scratching\/","title":{"rendered":"Regex.Match &#8211; this might save you 5 minutes of head-scratching"},"content":{"rendered":"<p>I often make use of the <code>Regex.IsMatch( string )<\/code> method to check if a regular expression finds a match in a given string, however I do not often use the <code>Match( string )<\/code> method which, unsurprisingly, returns an instance of <code>Match<\/code>. <code>Matches( string )<\/code> returns a <code>MatchCollection<\/code>. Very handy.<\/p>\n<p>  But what does the <code>Match( string )<\/code> method do if there is no match? You might be left scratching your head over this if you look at the documentation since it seems quite reluctant to reveal this precious secret. I initially wondered if it might just return <code>null<\/code>, but it doesn&#8217;t say so and this made me uneasy enough to take a little time to investigate further. If you look at the <code>Match<\/code> class itself you will find the <code>Success<\/code> property, which indicates whether or not the match succeeded. This seems to be exactly what you need, however I was still suspicious so I hacked together the world&#8217;s shortest NUnit test case and whacked a breakpoint on the call to <code>Match( string )<\/code> in order to inspect the return value. Much to my relief it turned out that my non-matching regular expression returned a match with <code>Success<\/code> set to <code>false<\/code>.<\/p>\n<p>  If you instead call <code>Matches( string )<\/code> with a string that does not match you will get back an empty <code>MatchCollection<\/code>, which is perfect and exactly what you would expect (the documentation is much clearer on this point, which is also good).<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I often make use of the Regex.IsMatch( string ) method to check if a regular expression finds a match in a given string, however I do not often use the Match( string ) method which, unsurprisingly, returns an instance of Match. Matches( string ) returns a MatchCollection. Very handy. But what does the Match( string&#8230;&hellip;<\/p>\n","protected":false},"author":221714,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-2353","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/221714"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2353"}],"version-history":[{"count":1,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2353\/revisions"}],"predecessor-version":[{"id":24322,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2353\/revisions\/24322"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2353"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}