{"id":8509,"date":"2022-09-14T00:41:14","date_gmt":"2022-09-13T19:11:14","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/"},"modified":"2022-09-14T00:41:14","modified_gmt":"2022-09-13T19:11:14","slug":"solved-recognizing-a-pattern-for-regular-expressions-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/","title":{"rendered":"[Solved] Recognizing a pattern for regular expressions [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-45930164\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"45930164\" data-parentid=\"45928515\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>In Java there are lots of ways you can code to get the contents between brackets (or any two specific characters for that matter) but you sort of want to dabble with a regular expression which can be rather slow for this sort of thing especially when dealing with multiple instances of bracket pairs within a supplied string.<\/p>\n<p>The basic code you want to gather up the contents contained between <strong>Curly Brackets<\/strong> could be something like this:<\/p>\n<pre><code>String myString = \"something + {SomeProductSet1}.count + {SomeOtherProductSet2}.amount &gt; \\n\" +\n                \"    {SomeProductSet3}.count + {SomeUSOC4}.amount\";\nMatcher match = Pattern.compile(\"\\\\{([^}]+)\\\\}\").matcher(myString);\nwhile(match.find()) {\n    System.out.println(match.group(1));    \n}\n<\/code><\/pre>\n<p>What the above Regular Expression means:<\/p>\n<ul>\n<li><code>\\\\{<\/code> Open Curly Bracket character <code>{<\/code><\/li>\n<li><code>(<\/code> start match group<\/li>\n<li><code>[<\/code> one of these characters<\/li>\n<li><code>^<\/code> not the following character<\/li>\n<li><code>}<\/code> with the previous <code>^<\/code>, this means &#8220;every character except the Close<br \/>\nCurly Bracket <code>}<\/code>&#8220;<\/li>\n<li><code>+<\/code> one of more other characters from the <code>[]<\/code> set<\/li>\n<li><code>)<\/code> stop match group<\/li>\n<li><code>\\\\}<\/code> literal Closing Curly Bracket <code>}<\/code><\/li>\n<\/ul>\n<p>If it were me, I would create a method to house this code so that it can be used for other bracket types as well like: Parentheses (), Square Brackets [], Curly Brackets {} (as shown in code), Chevron Brackets &lt;&gt;, or even between any two supplied characters like: \/&#8230;\/ or %&#8230;% or maybe even A&#8230;A. See the example method below which demonstrates this. <\/p>\n<p>In the example code above it would be within the <strong>while<\/strong> loop where you would handle each substring found between each set of brackets. You will of course require a mechanism to determine which substring detected is to be replaced with whatever string like perhaps a multidimensional Array, or perhaps even a custom dialog which would display the found substring between each bracket and allow the User to select its replacement from perhaps a Combo Box with a <strong>Do All<\/strong> option Check Box. There are of course several options here for how and what you want to handle each found substring between each set of brackets.<\/p>\n<p>Here is a method example which demonstrates what we&#8217;ve discussed here. It is well commented:<\/p>\n<pre><code>public String replaceBetween(String inputString, String openChar, \n                             String closeChar, String[][] replacements) {\n    \/\/ If the supplied input String contains nothing\n    \/\/ then return a Null String (\"\").\n    if (inputString.isEmpty()) { return \"\"; }\n\n    \/\/ Declare a string to hold the input string, this way\n    \/\/ we can freely manipulate it without jeopordizing the\n    \/\/ original input string.\n    String inString = inputString;\n\n    \/\/ Set the escape character (\\) for RegEx special Characters\n    \/\/ for both the openChar and closeChar parameters in case\n    \/\/ a character in each was supplied that is a special RegEx\n    \/\/ character. We'll use RegEx to do this.\n    Pattern regExChars = Pattern.compile(\"[{}()\\\\[\\\\].+*?^$\\\\\\\\|]\");\n    String opnChar = regExChars.matcher(openChar).replaceAll(\"\\\\\\\\$0\");\n    String clsChar = regExChars.matcher(closeChar).replaceAll(\"\\\\\\\\$0\");\n\n    \/\/ Create our Pattern to find the items contained between\n    \/\/ the characters tht was supplied in the openChar and\n    \/\/ closeChar parameters.\n    Matcher m = Pattern.compile(opnChar + \"([^\" + closeChar + \"]+)\" + clsChar).matcher(inString);\n\n    \/\/ Iterate through the located items...\n    while(m.find()) {\n        String found = m.group(1);\n        \/\/ Lets see if the found item is contained within\n        \/\/ our supplied 2D replacement items Array... \n        for (int i = 0; i &lt; replacements.length; i++) {\n            \/\/ Is an item found in the array?\n            if (replacements[i][0].equals(found)) {\n                \/\/ Yup... so lets replace the found item in our \n                \/\/ input string with the related element in our\n                \/\/ replacement array.\n                inString = inString.replace(openChar + found + closeChar, replacements[i][1]);\n            }\n        }\n    }\n    \/\/ Return the modified input string.\n    return inString;\n}\n<\/code><\/pre>\n<p>To use this method you might do this:<\/p>\n<pre><code>\/\/ Our 2D replacement array. In the first column we place \n\/\/ the substrings we would like to find within our input \n\/\/ string and in the second column we place what we want \n\/\/ to replace the item in the first column with if it's \n\/\/ found.\nString[][] replacements = {{\"SomeProductSet1\", \"[abc]\"}, \n                           {\"SomeOtherProductSet2\", \"[xyz]\"},\n                           {\"SomeProductSet3\", \"[xom]\"},\n                           {\"SomeUSOC4\", \"[ytkd]\"}};\n\n\/\/ The string we want to modify (the input string):\nString example = \"something + {SomeProductSet1}.count + {SomeOtherProductSet2}.amount &gt; \\n\" +\n                 \"    {SomeProductSet3}.count + {SomeUSOC4}.amount\";\n\n\/\/ Lets make the call and place the returned result\n\/\/ into a new variable...\nString newString = replaceBetween(example, \"{\", \"}\", replacements);\n\n\/\/ Display the new string variable contents\n\/\/ in Console.\nSystem.out.println(newString);\n<\/code><\/pre>\n<p>The Console should display:<\/p>\n<pre><code>something + [abc].count + [xyz].amount &gt; \n    [xom].count + [ytkd].amount\n<\/code><\/pre>\n<p>Notice how it also replaces the Curly Brackets? This appears to be one of your requirements but can be easily modified to just replace the substring between the brackets. Perhaps you can modify this method (if you like) to do this optionally and as yet another added optional feature&#8230;.allow it to ignore letter case.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Recognizing a pattern for regular expressions [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] In Java there are lots of ways you can code to get the contents between brackets (or any two specific characters for that matter) but you sort of want to dabble with a regular expression which can be rather slow for this sort of thing especially when dealing with multiple instances of bracket pairs &#8230; <a title=\"[Solved] Recognizing a pattern for regular expressions [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/\" aria-label=\"More on [Solved] Recognizing a pattern for regular expressions [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[323,2420,347],"class_list":["post-8509","post","type-post","status-publish","format-standard","hentry","category-solved","tag-java","tag-pattern-matching","tag-regex"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Recognizing a pattern for regular expressions [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Recognizing a pattern for regular expressions [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] In Java there are lots of ways you can code to get the contents between brackets (or any two specific characters for that matter) but you sort of want to dabble with a regular expression which can be rather slow for this sort of thing especially when dealing with multiple instances of bracket pairs ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-13T19:11:14+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Recognizing a pattern for regular expressions [closed]\",\"datePublished\":\"2022-09-13T19:11:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/\"},\"wordCount\":393,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"java\",\"pattern-matching\",\"regex\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/\",\"name\":\"[Solved] Recognizing a pattern for regular expressions [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-13T19:11:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Recognizing a pattern for regular expressions [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Recognizing a pattern for regular expressions [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Recognizing a pattern for regular expressions [closed] - JassWeb","og_description":"[ad_1] In Java there are lots of ways you can code to get the contents between brackets (or any two specific characters for that matter) but you sort of want to dabble with a regular expression which can be rather slow for this sort of thing especially when dealing with multiple instances of bracket pairs ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/","og_site_name":"JassWeb","article_published_time":"2022-09-13T19:11:14+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Recognizing a pattern for regular expressions [closed]","datePublished":"2022-09-13T19:11:14+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/"},"wordCount":393,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["java","pattern-matching","regex"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/","name":"[Solved] Recognizing a pattern for regular expressions [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-13T19:11:14+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-recognizing-a-pattern-for-regular-expressions-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Recognizing a pattern for regular expressions [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/8509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=8509"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/8509\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=8509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=8509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=8509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}