{"id":21358,"date":"2022-11-13T05:47:30","date_gmt":"2022-11-13T00:17:30","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/"},"modified":"2022-11-13T05:47:30","modified_gmt":"2022-11-13T00:17:30","slug":"solved-how-to-extract-integer-after-sign-using-ruby","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/","title":{"rendered":"[Solved] How to extract integer after &#8220;=&#8221; sign using ruby"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-33440467\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"33440467\" data-parentid=\"33413211\" data-score=\"0\" data-position-on-page=\"2\" data-highest-scored=\"0\" data-question-has-accepted-highest-score=\"0\" itemprop=\"suggestedAnswer\" 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>I&#8217;d do something like this:<\/p>\n<pre><code>string = &lt;&lt;EOT\nvar i=0;\nvar recharge=[];\nvar recharge_text=[];\nvar recharge_String=\"\";\nvar mrp=\"\";\nvar talktime=\"\";\nvar validity=\"\";\nvar mode=\"\";mrp='1100';\ntalktime=\"1200.00\";\nvalidity='NA';\nmode=\"E-Recharge\";\nif(typeof String.prototype.trim !== 'function') {\nString.prototype.trim = function() {\nreturn this.replace(\/^ +| +$\/g, '');\n}\n}\nmrp=mrp.trim();\nif(isNaN(mrp))\n{\nrecharge_text.push({MRP:mrp, Talktime:talktime, Validity:validity ,Mode:mode});\n}\nelse\n{\nmrp=parseInt(mrp);\nrecharge.push({MRP:mrp, Talktime:talktime, Validity:validity ,Mode:mode});\n}\nmrp='2200';\ntalktime=\"2400.00\";\nEOT\n\nhits = string.scan(\/(?:mrp|talktime)='[\\d.]+'\/)\n# =&gt; [\"mrp='1100'\", \"talktime=\"1200.00\"\", \"mrp='2200'\", \"talktime=\"2400.00\"\"]\n<\/code><\/pre>\n<p>This gives us an array of hits using <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/ruby-doc.org\/core-2.2.3\/String.html#method-i-scan\"><code>scan<\/code><\/a>, where the pattern <code>\/(?:mrp|talktime)='[\\d.]+'\/<\/code> matched in the string. Figuring out how the pattern works is left as an exercise for the user, but Ruby&#8217;s <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/ruby-doc.org\/core-2.2.3\/Regexp.html\">Regexp<\/a> documentation explains it all.<\/p>\n<p>Cleaning that up to be a bit more useful:<\/p>\n<pre><code>hash = hits.map{ |s|\n  str, val = s.split('=')\n  [str, val.delete(\"'\")]\n}.each_with_object(Hash.new { |h, k| h[k] = [] }){ |(str, val), h| h[str] &lt;&lt; val }\n<\/code><\/pre>\n<p>You also need to read about <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/ruby-doc.org\/core-2.2.3\/Enumerable.html#method-i-each_with_object\"><code>each_with_object<\/code><\/a> and what&#8217;s happening with <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/ruby-doc.org\/core-2.2.3\/Hash.html#method-c-new\"><code>Hash.new<\/code><\/a> as those are important concepts to learn in Ruby.<\/p>\n<p>At this point, <code>hash<\/code> is a hash of arrays:<\/p>\n<pre><code>hash # =&gt; {\"mrp\"=&gt;[\"1100\", \"2200\"], \"talktime\"=&gt;[\"1200.00\", \"2400.00\"]}\n<\/code><\/pre>\n<p>You can easily extract a particular variable&#8217;s values, and can correlate them if need be. <\/p>\n<hr>\n<blockquote>\n<p>what if i get a string instead of integer next to &#8220;=&#8221; sign?<\/p>\n<p>&#8230;<\/p>\n<p><code>string.scan(\/(?:tariff)='[\\p{Print}]+'\/)<\/code><\/p>\n<\/blockquote>\n<p>It&#8217;s important to understand what the pattern is doing. The regular expression engine has some gotchas that can drastically affect the speed of a search, so indiscriminately throwing in things without understanding what they do can be very costly.<\/p>\n<p>When using <code>(?:...)<\/code>, you&#8217;re creating a non-capturing group. When you only have one item you&#8217;re matching it&#8217;s not necessary, nor is it particularly desirable since it&#8217;s making the engine do more work. The only time I&#8217;d do that is when I need to refer back to what the capture was, but since you have only one possible thing it&#8217;ll match that becomes a moot-point. So, your pattern should be reduced to:<\/p>\n<pre><code>\/tariff=\"[\\p{Print}]+\"\/\n<\/code><\/pre>\n<p>Which, when used, results in:<\/p>\n<pre><code>%(tariff=\"abcdef abc a\").scan(\/tariff=\"[\\p{Print}]+\"\/) \n# =&gt; [\"tariff=\"abcdef abc a\"\"]\n<\/code><\/pre>\n<p>If you want to capture all non-empty occurrences of the string being assigned, it&#8217;s easier than what you&#8217;re doing. I&#8217;d use something like:<\/p>\n<pre><code>%(tariff=\"abcdef abc a\").scan(\/tariff=\".+\"\/) \n# =&gt; [\"tariff=\"abcdef abc a\"\"]\n\n%(tariff=\"abcdef abc a\").scan(\/tariff=\"[^\"]+'\/) \n# =&gt; [\"tariff=\"abcdef abc a\"\"]\n<\/code><\/pre>\n<p>The second is more rigorous, and possible safer as it won&#8217;t be tricked by an line that has multiple single-quotes:<\/p>\n<pre><code>%(tariff=\"abcdef abc a\", 'foo').scan(\/tariff=\".+\"\/) \n# =&gt; [\"tariff=\"abcdef abc a\", 'foo'\"]\n\n%(tariff=\"abcdef abc a\", 'foo').scan(\/tariff=\"[^\"]+'\/) \n# =&gt; [\"tariff=\"abcdef abc a\"\"]\n<\/code><\/pre>\n<p>Why that works is for you to figure out.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">3<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to extract integer after &#8220;=&#8221; sign using ruby <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I&#8217;d do something like this: string = &lt;&lt;EOT var i=0; var recharge=[]; var recharge_text=[]; var recharge_String=&#8221;&#8221;; var mrp=&#8221;&#8221;; var talktime=&#8221;&#8221;; var validity=&#8221;&#8221;; var mode=&#8221;&#8221;;mrp=&#8217;1100&#8242;; talktime=&#8221;1200.00&#8243;; validity=&#8217;NA&#8217;; mode=&#8221;E-Recharge&#8221;; if(typeof String.prototype.trim !== &#8216;function&#8217;) { String.prototype.trim = function() { return this.replace(\/^ +| +$\/g, &#8221;); } } mrp=mrp.trim(); if(isNaN(mrp)) { recharge_text.push({MRP:mrp, Talktime:talktime, Validity:validity ,Mode:mode}); } else { mrp=parseInt(mrp); &#8230; <a title=\"[Solved] How to extract integer after &#8220;=&#8221; sign using ruby\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/\" aria-label=\"More on [Solved] How to extract integer after &#8220;=&#8221; sign using ruby\">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":[455,615],"class_list":["post-21358","post","type-post","status-publish","format-standard","hentry","category-solved","tag-ruby","tag-ruby-on-rails"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How to extract integer after &quot;=&quot; sign using ruby - 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-how-to-extract-integer-after-sign-using-ruby\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to extract integer after &quot;=&quot; sign using ruby - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I&#8217;d do something like this: string = &lt;&lt;EOT var i=0; var recharge=[]; var recharge_text=[]; var recharge_String=&quot;&quot;; var mrp=&quot;&quot;; var talktime=&quot;&quot;; var validity=&quot;&quot;; var mode=&quot;&quot;;mrp=&#039;1100&#039;; talktime=&quot;1200.00&quot;; validity=&#039;NA&#039;; mode=&quot;E-Recharge&quot;; if(typeof String.prototype.trim !== &#039;function&#039;) { String.prototype.trim = function() { return this.replace(\/^ +| +$\/g, &#039;&#039;); } } mrp=mrp.trim(); if(isNaN(mrp)) { recharge_text.push({MRP:mrp, Talktime:talktime, Validity:validity ,Mode:mode}); } else { mrp=parseInt(mrp); ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-13T00:17:30+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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to extract integer after &#8220;=&#8221; sign using ruby\",\"datePublished\":\"2022-11-13T00:17:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/\"},\"wordCount\":305,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"ruby\",\"ruby-on-rails\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/\",\"name\":\"[Solved] How to extract integer after \\\"=\\\" sign using ruby - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-13T00:17:30+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to extract integer after &#8220;=&#8221; sign using ruby\"}]},{\"@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=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How to extract integer after \"=\" sign using ruby - 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-how-to-extract-integer-after-sign-using-ruby\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to extract integer after \"=\" sign using ruby - JassWeb","og_description":"[ad_1] I&#8217;d do something like this: string = &lt;&lt;EOT var i=0; var recharge=[]; var recharge_text=[]; var recharge_String=\"\"; var mrp=\"\"; var talktime=\"\"; var validity=\"\"; var mode=\"\";mrp='1100'; talktime=\"1200.00\"; validity='NA'; mode=\"E-Recharge\"; if(typeof String.prototype.trim !== 'function') { String.prototype.trim = function() { return this.replace(\/^ +| +$\/g, ''); } } mrp=mrp.trim(); if(isNaN(mrp)) { recharge_text.push({MRP:mrp, Talktime:talktime, Validity:validity ,Mode:mode}); } else { mrp=parseInt(mrp); ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/","og_site_name":"JassWeb","article_published_time":"2022-11-13T00:17:30+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to extract integer after &#8220;=&#8221; sign using ruby","datePublished":"2022-11-13T00:17:30+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/"},"wordCount":305,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["ruby","ruby-on-rails"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/","name":"[Solved] How to extract integer after \"=\" sign using ruby - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-13T00:17:30+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-extract-integer-after-sign-using-ruby\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to extract integer after &#8220;=&#8221; sign using ruby"}]},{"@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=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","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\/21358","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=21358"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/21358\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=21358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=21358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=21358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}