{"id":1223,"date":"2011-12-23T16:59:00","date_gmt":"2011-12-24T00:59:00","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=1223"},"modified":"2011-12-24T07:40:04","modified_gmt":"2011-12-24T15:40:04","slug":"regex-and-mysql","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=1223","title":{"rendered":"REGEX and MySQL"},"content":{"rendered":"<p>I\u2019m working on my words database using phpMyAdmin and thought others might benefit from some of the search expressions.<\/p>\n<p>Find words that start with &#8216;Wr&#8217; or &#8216;wr&#8217; and the CVC category is not null.<br \/>\n<pre><code class=\"\u201dsmaller\u201d preserve-code-formatting\">\nSELECT *&nbsp;&nbsp;FROM `words` WHERE `word` REGEXP &#039;^[Ww]r[a-z]*&#039; \nAND `CVC` IS NOT NULL\n<\/code><\/pre><\/p>\n<p>This gives me words like, Rhoda, Rhone, rhyme because I\u2019m asking to start at the beginning of the word &#8216;^&#8217; then look for either a capital or lowercase w &#8216;[Ww] followed by an &#8216;r&#8217; and then any letter &#8216;[a-z]&#8217; that occurs zero or more times &#8216;*&#8217;. I\u2019m further restricting the search to words that have an entry in the CVC category. In phpMyAdmin you can paste this into the SQL editing field or just use the REGEX in the word field and set it to REGEXP.<\/p>\n<p>Here\u2019s one looking for all the words that end in the letters mb and the CVC category is not null.<br \/>\n<pre><code class=\"\u201dsmaller\u201d preserve-code-formatting\">\nSELECT *&nbsp;&nbsp;FROM `words` WHERE `word` REGEXP &#039;mb$&#039; \nAND `CVC` IS NOT NULL\n<\/code><\/pre><\/p>\n<p>The &#8216;$&#8217; means the end of the word.<\/p>\n<p>This search yields words like climb, plumb, thumb.<\/p>\n<p>This search looks for words like gnome and gnat. If you want you can leave out the [a-z]* and you\u2019ll get the same result.<br \/>\n<pre><code class=\"\u201dsmaller\u201d preserve-code-formatting\">\nSELECT *&nbsp;&nbsp;FROM `words_for_slps` WHERE `word` REGEXP &#039;^[Gg]n[a-z]*&#039; \nAND `CVC` IS NOT NULL\n<\/code><\/pre><\/p>\n<p>This one\u2019s a little more complicated, I\u2019m looking for words like tongue and dengue but don\u2019t know if there are words with gue in the middle\u2014like tongues.<br \/>\n<pre><code class=\"\u201dsmaller\u201d preserve-code-formatting\">\nSELECT *&nbsp;&nbsp;FROM `words_for_slps` WHERE `word` REGEXP &#039;^[a-zA-z]*ngue[a-z]*&#039; \nAND `CVC` IS NOT NULL\n<\/code><\/pre><\/p>\n<p>You can&#8217;t do OR queries in phpMyAdmin interface everything is an AND. But if you do a query, then edit the SQL it is easy to make the query into an OR.<\/p>\n<p>This is the original query to find words where the grade and level are NULL.<\/p>\n<p><pre><code class=\"\u201dsmaller\u201d preserve-code-formatting\">\nSELECT *&nbsp;&nbsp;FROM `words_for_slps` WHERE `level` IS NULL AND `grade` IS NULL \nAND `CVC` IS NOT NULL\n<\/code><\/pre><\/p>\n<p>And here is the same code where all I changed is the AND to OR and just to be sure I\u2019m getting the right results, I grouped the OR with parentheses. (I can never the default rules for the different languages so I always use parentheses so I\u2019m sure I get what I want.)<br \/>\n<pre><code class=\"\u201dsmaller\u201d preserve-code-formatting\">\nSELECT *&nbsp;&nbsp;FROM `words_for_slps` WHERE (`level` IS NULL OR `grade` IS NULL) \nAND `CVC` IS NOT NULL\n<\/code><\/pre><\/p>\n<p>I like this result but want to look deal with the shorter words first and then work my way up to the harder ones. I also want to skip the words that do not follow the rules (DNFR). Use <> for not equal.<\/p>\n<p><pre><code class=\"\u201dsmaller\u201d preserve-code-formatting\">\nSELECT *&nbsp;&nbsp;FROM `words_for_slps` WHERE (`level` IS NULL OR `grade` IS NULL) \nAND `CVC` IS NOT NULL AND `CVC` &lt;&gt; &#039;DNFR&#039; ORDER BY LENGTH(word)\n<\/code><\/pre><\/p>\n<p>Here&#8217;s the code I use to find all of the conosonants<br \/>\n<pre><code class=\"\u201dsmaller\u201d preserve-code-formatting\">\n([bcdfghjklmnpqrstvwxz]|ch|sh|zh|th|wh|ng|qu|wh|tch|ph)\n<\/code><\/pre><\/p>\n<p>And this finds all the vowels.<br \/>\n<pre><code class=\"\u201dsmaller\u201d preserve-code-formatting\">\n(a|e|i|o|u|aw|ow|ie|ae|oe|oy|oo|uu|y)\n<\/code><\/pre><\/p>\n<p>And this finds all of the CVCCV words. Note the {2} that finds exactly two occurrences of the previous search string.<br \/>\n<pre><code class=\"\u201dsmaller\u201d preserve-code-formatting\">\nSELECT *&nbsp;&nbsp;FROM `words_for_slps` WHERE `word`\n^([bcdfghjklmnpqrstvwxz]|ch|sh|zh|th|wh|ng|qu|wh|tch|ph)\n(a|e|i|o|u|aw|ow|ie|ae|oe|oy|oo|uu|y)\n([bcdfghjklmnpqrstvwxz]|ch|sh|zh|th|wh|ng|qu|wh|tch|ph){2}\n(a|e|i|o|u|aw|ow|ie|ae|oe|oy|oo|uu|y)$\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019m working on my words database using phpMyAdmin and thought others might benefit from some of the search expressions. Find words that start with &#8216;Wr&#8217; or &#8216;wr&#8217; and the CVC category is not null. SELECT *&nbsp;&nbsp;FROM `words` WHERE `word` REGEXP &#039;^[Ww]r[a-z]*&#039; AND `CVC` IS NOT NULL This gives me words like, Rhoda, Rhone, rhyme because &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=1223\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">REGEX and MySQL<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1223","post","type-post","status-publish","format-standard","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1223"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1223\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}