{"id":4255,"date":"2022-02-10T14:05:19","date_gmt":"2022-02-10T14:05:19","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4255"},"modified":"2022-02-10T14:05:21","modified_gmt":"2022-02-10T14:05:21","slug":"13f-wxwidgets-regular-expressions","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/02\/10\/13f-wxwidgets-regular-expressions\/","title":{"rendered":"13f. wxWidgets &#8211; Regular Expressions"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>Regular expressions is a very complex subject and it is used in a lot of languages. We cannot go into the details of how regular expressions work, but for the sake of this tutorial series, we can say that wxWidgets supports regular expressions and we can use them for string parsing and extraction. The <em>wxRegEx<\/em> class is the main class which handles regular expressions.<\/p>\n\n\n\n<p>We have two sample codes below. The first one extracts any string that is all capital letters from a sample text. The second one is a little more complex and extracts integers from a text repeatedly unless it cannot find any.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">regexpsimple.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n#include &lt;wx\/regex.h&gt;\n#include &lt;wx\/string.h&gt;\n\nint main(int argc, char**argv) {\n\n\twxInitialize();\n\twxString pattern = wxT(&quot;(&#x5B;A-Z]+)&quot;);\n\twxRegEx *regExp = new wxRegEx(pattern);\n\twxString text = wxT(&quot;some of THIS text is in capitals&quot;);\n\twxPuts(text);\n\tbool result = regExp-&gt;Compile(pattern);\n\tif (!result) {\n\t\twxPuts(&quot;Compile failed&quot;);\n\t\treturn -1;\n\t}\n\tif (regExp-&gt;Matches(text)) {\n\t\twxPrintf(wxT(&quot;Match=%s\\n&quot;),regExp-&gt;GetMatch(text, 0));\n\t\t\n\t}\n\tdelete regExp;\n\twxPuts(wxT(&quot;done&quot;));\n\twxUninitialize();\n}\n\n<\/pre><\/div>\n\n\n<p>The output is given below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"644\" height=\"59\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-10-19-32-40.png\" alt=\"\" class=\"wp-image-4258\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-10-19-32-40.png 644w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-10-19-32-40-620x57.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-10-19-32-40-300x27.png 300w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">regexp.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n#include &lt;wx\/regex.h&gt;\n#include &lt;wx\/string.h&gt;\n\nint main(int argc, char**argv) {\n\n\twxInitialize();\n\twxString pattern = wxT(&quot;(&#x5B;0-9]+)&quot;);\n\twxRegEx *regExpNumber = new wxRegEx(pattern);\n\twxString text = wxT(&quot;this is a text 1817,128128 some words 32withtext6124.word09&quot;);\n\twxPuts(text);\n\tbool result = regExpNumber-&gt;Compile(pattern);\n\tif (!result) {\n\t\twxPuts(&quot;Compile failed&quot;);\n\t\treturn -1;\n\t}\n\tif (regExpNumber-&gt;Matches(text)) {\n\t\twxPuts(&quot;Matches are there&quot;);\n\t\tsize_t index = 0;\n\t\tsize_t len = 0;\n\t\tsize_t start = 0;\n\n\t\twhile(regExpNumber-&gt;GetMatch(&amp;start, &amp;len, 0)) {\n\t\t\twxPrintf(wxT(&quot;Match=%s\\n&quot;),regExpNumber-&gt;GetMatch(text, 0));\n\t\t\tindex = start+len;\n\t\t\ttext = text.Mid(index);\n\t\t\twxPuts(text);\n\t\t\tif (!regExpNumber-&gt;Matches(text)) {\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t}\n\t\t\n\t}\n\tdelete regExpNumber;\n\twxPuts(wxT(&quot;done&quot;));\n\twxUninitialize();\n}\n\n<\/pre><\/div>\n\n\n<p>The output is shown below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"511\" height=\"202\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-10-19-33-47.png\" alt=\"\" class=\"wp-image-4259\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-10-19-33-47.png 511w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-10-19-33-47-300x119.png 300w\" sizes=\"auto, (max-width: 511px) 100vw, 511px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW Regular expressions is a very complex subject and it is used in a lot of languages. We cannot go into the details of how <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/02\/10\/13f-wxwidgets-regular-expressions\/\" title=\"13f. wxWidgets &#8211; Regular Expressions\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3595,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,365],"tags":[],"class_list":["post-4255","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc","category-wxwidgets"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4255"}],"version-history":[{"count":4,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4255\/revisions"}],"predecessor-version":[{"id":4261,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4255\/revisions\/4261"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/3595"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}