{"id":1020,"date":"2013-12-06T00:09:34","date_gmt":"2013-12-06T05:09:34","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=1020"},"modified":"2013-12-06T00:09:34","modified_gmt":"2013-12-06T05:09:34","slug":"using-group-expressions-in-regular-expression-pattern-matching","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2013\/12\/using-group-expressions-in-regular-expression-pattern-matching\/","title":{"rendered":"Using group expressions in regular expression pattern matching"},"content":{"rendered":"<p>I&#8217;ve used group expressions in regexes many times, but only for replacement. Yesterday I learned that they can also be used for matching.<\/p>\n<p>For example, let&#8217;s say you have the text:<\/p>\n<blockquote><p>Banananananas don&#8217;t grow in Mississississippi because banananas are afraid of getting turned into Missississippi&#8217;s famous bananana pudding.<\/p><\/blockquote>\n<p>The following regular expression will find instances of <em>iss<\/em> or <em>an<\/em> that are repeated more than twice.<\/p>\n<p><code>(iss|an)\\1\\1+<\/code><\/p>\n<p>You can use <code>\\1\\1<\/code> as the replacement (or <code>$1$1<\/code> in Dreamweaver, which uses backslashes to identify groups in match expressions, but dollar signs to represent groups in replace expressions) to turn the misspelled words into <em>Mississippi<\/em> and <em>banana(s)<\/em>.<\/p>\n<p>Another example might be applying consistent formatting to phone numbers or dates.<\/p>\n<p><strong>Phone numbers<\/strong><br \/>\nLet&#8217;s say you usually use 555-555-1212 as the format for phone numbers and sometimes you use 555.555.1212, but the new trend is to use spaces instead of dashes or dots as separators:<\/p>\n<p><strong>Find:<\/strong> ([\\d]{3})([-\\.])([\\d]{3})\\2([\\d]{4})<br \/>\n<strong>Replace:<\/strong> \\1 \\3 \\4<\/p>\n<p><strong>Dates<\/strong><br \/>\nLet&#8217;s say you usually use 12\/5\/2013 as the format for dates, dabbled with 12.5.2013, but now you&#8217;ve decided that dashes are clearer:<\/p>\n<p><strong>Find:<\/strong> ([\\d]{1,2})([\\.\/])([\\d]{1,2})\\2([\\d]{4})<br \/>\n<strong>Replace:<\/strong> \\1-\\3-\\4<\/p>\n<p>In both cases you could just repeat the bracketed character class, but then you could end up matching strings you didn&#8217;t intend to:<\/p>\n<ul>\n<li>555-555.1212<\/li>\n<li>12.5\/2013<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve used group expressions in regexes many times, but only for replacement. Yesterday I learned that they can also be used for matching. For example, let&#8217;s say you have the text: Banananananas don&#8217;t grow in Mississississippi because banananas are afraid of getting turned into Missississippi&#8217;s famous bananana pudding. The following regular expression will find instances &hellip; <a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2013\/12\/using-group-expressions-in-regular-expression-pattern-matching\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using group expressions in regular expression pattern matching<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[232],"tags":[326,327,325],"class_list":["post-1020","post","type-post","status-publish","format-standard","hentry","category-tips-tricks","tag-regex","tag-regexp","tag-regular-expressions"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/1020","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=1020"}],"version-history":[{"count":3,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/1020\/revisions"}],"predecessor-version":[{"id":1031,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/1020\/revisions\/1031"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=1020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=1020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=1020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}