{"id":1890,"date":"2010-10-14T20:57:00","date_gmt":"2010-10-14T15:27:00","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1890"},"modified":"2016-12-19T15:28:13","modified_gmt":"2016-12-19T09:58:13","slug":"regex-in-grails-createcriteria","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/regex-in-grails-createcriteria\/","title":{"rendered":"Regex in grails CreateCriteria"},"content":{"rendered":"<p>Sometimes we need to retrieve the result on the basis of some regular expression pattern which could be applied to a column. In such situations its better to use a rlike method provided by Grails CreateCriteria. Using rlike method it&#8217;s easy to retrieve Result based upon the Required Pattern.<br \/>\nrlike Method is Similar to like, but uses a regex. Only supported on Oracle and MySQL.<\/p>\n<p>Syntax: rlike(&#8220;columnName&#8221;,\/Regular Expression\/)<br \/>\ne.g.:<br \/>\nrlike(&#8220;holderFirstName&#8221;,\/Steph.+\/)<\/p>\n<pre lang=\"groovy\">\r\n.     :  Checks the matched expression for 0 or 1 Characters\/Numerics\r\n+    :  Checks the matched expression for 1 or More Characters\/Numerics\r\n*     :  Checks the matched expression for 0 or More Characters\/Numerics\r\n[0-9]: Checks the matched expression for 1 or more Numeric(Between 0 to 9)\r\n[a-z]: Checks the matched expression for 1 or more Character(Between a to z or A to Z)\r\n^     :    Checks that the matched expression should start with the letters\/numbers following it \r\n$     :    Checks that the matched expression should end with the letters\/numbers before it\r\n<\/pre>\n<p>Example1:<\/p>\n<pre lang=\"groovy\">\r\ndef userList=User.createCriteria().list{\r\n        projections {\r\n                property(\"firstName\")\r\n                      }\r\n      \/\/check for pattern that start with F and has 5 letters in it and ends with digit\r\n     rlike(\"firstName\",\/^F.....[0-9]$\/)\r\n }\r\nOutput: [First10, First11]\r\n<\/pre>\n<p>\nExample2:If we replace rlike in Example1 with this:<\/p>\n<pre lang=\"groovy\">\r\n \/\/check for pattern that start with F and has 0 or more letters in it and ends with digit\r\nrlike(\"firstName\",\/^F.*[0-9]$\/)\r\nOutput:[First0, First1, First2, First3,First10, First11]\r\n<\/pre>\n<p>\nExample3:If we replace rlike in Example1 with this:<\/p>\n<pre lang=\"groovy\">\r\n \/\/check for pattern that start with L and has 0 or more letters in it and ends with digit\r\nrlike(\"lastName\",\/^L*[0-9]$\/)\r\nOutput: []\r\n<\/pre>\n<p>This Expression won&#8217;t works. It will return an Empty List.<br \/>\n<br \/>\nExample4:If we replace rlike in Example1 with this:<\/p>\n<pre lang=\"groovy\">\r\n \/\/check for pattern that start with First and has 1 or more letters in it\r\nrlike(\"firstName\",\/First.+\/)\r\nOutput:[First0, First1, First2, First3,First10, First11]\r\n<\/pre>\n<p><\/p>\n<p>Example5:<\/p>\n<pre lang=\"groovy\">\r\ndef  userList=User.createCriteria().list{\r\n        projections {\r\n                property(\"firstName\")\r\n                property(\"lastName\")\r\n         }\r\n  rlike(\"lastName\",\/^L.*[0-9]$\/)       \/\/  rlike(\"lastName\",\/^L.*[0123456789]$\/)\r\n  }\r\nOutput:[[First0, Last 0], [First1, Last 1], [First2, Last 2], [First3, Last 3],\r\n[First10, Last 10], [First11, Last 11]]\r\n<\/pre>\n<p>For more information you can visit this site:<br \/>\n\t <a href=\"http:\/\/www.grails.org\/doc\/latest\/ref\/Domain%20Classes\/createCriteria.html\">http:\/\/www.grails.org\/doc\/latest\/ref\/Domain%20Classes\/createCriteria.html<\/a><br \/>\n<br \/>\nHope it Helps!!!<\/p>\n<p>Regards,<br \/>\nGautam Malhotra<br \/>\nIntelligrape Software<br \/>\nhttp:\/\/www.tothenew.com\/blog\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes we need to retrieve the result on the basis of some regular expression pattern which could be applied to a column. In such situations its better to use a rlike method provided by Grails CreateCriteria. Using rlike method it&#8217;s easy to retrieve Result based upon the Required Pattern. rlike Method is Similar to like, [&hellip;]<\/p>\n","protected":false},"author":25,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[7],"tags":[428,429,430],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1890"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1890"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1890\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}