{"id":1750,"date":"2010-10-14T22:29:00","date_gmt":"2010-10-14T16:59:00","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1750"},"modified":"2015-05-19T17:47:20","modified_gmt":"2015-05-19T12:17:20","slug":"regex-to-match-exact-word-in-a-string","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/regex-to-match-exact-word-in-a-string\/","title":{"rendered":"Regex to match exact word in a string"},"content":{"rendered":"<p>Hi,<\/p>\n<p>In my recent grails project, i needed to check the occurrence of a word in a string. I could have used contains() method of groovy , but the problem with contains() is that it also matches the word if the word is contained as a substring in any word.<br \/>\nI searched a lot, but nothing worked out so i decided to use Regular expression for it. With the help of my colleague,we came out with a simple regex to find the occurrence of exact word in a long string, even if the word is surrounded by comma, full-stop or some other character.<\/p>\n<p>The regex is:-<\/p>\n<pre lang=\"groovy\">\r\n     String matchingWord=\"string\"  \/\/ word to find\r\n     String longString=\"it is a very long string. find the exact word in it\"  \r\n\r\n \/\/ method to return the result\r\n\r\n    def pattern = \/\\b${matchingWord}\\b\/\r\n    def matcher = longString =~ pattern\r\n    return matcher.getCount() ? true : false\r\n\r\n\r\n<\/pre>\n<p>The method returns true if the exact word is found in the string. But it will return false if the word is occurring as substring.<\/p>\n<p>It worked in my case.<br \/>\nHope it helps.<\/p>\n<p>Vishal Sahu<br \/>\nvishal@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, In my recent grails project, i needed to check the occurrence of a word in a string. I could have used contains() method of groovy , but the problem with contains() is that it also matches the word if the word is contained as a substring in any word. I searched a lot, but [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":30},"categories":[7],"tags":[4840,431,432],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1750"}],"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\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1750"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1750\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}