{"id":1929,"date":"2016-02-28T10:22:48","date_gmt":"2016-02-28T15:22:48","guid":{"rendered":"http:\/\/bitpost.com\/news\/?p=1929"},"modified":"2016-02-28T10:58:21","modified_gmt":"2016-02-28T15:58:21","slug":"reliable-c-regular-expressions-in-10-minutes","status":"publish","type":"post","link":"https:\/\/bitpost.com\/news\/2016\/reliable-c-regular-expressions-in-10-minutes\/","title":{"rendered":"Reliable C++ regular expressions in 10 minutes"},"content":{"rendered":"<p>I gave myself an hour to add multi-line search-and-replace support in my code. \u00a0It took 3, and C++11 std::regex_replace() gave me a hard crash with inline replacement. \u00a0The syntax can be annoying (for example, using double escape), so for reference, here&#8217;s a jumpstart.<\/p>\n<p>Given:<\/p>\n<pre><code>using namespace std;\r\nstring str = \"Lots of text to search\\nacross lines\";\r\nstring from = \"text[\\\\s\\\\S]*lines\";\r\nstring to = \"rainbows\";<\/code><\/pre>\n<p>Boost happily did the job:<\/p>\n<pre><code>#include <boost\/regex.hpp>\r\n boost::regex reg;\r\n reg.assign(from);\r\n str = boost::regex_replace(str,reg,to,boost::match_default);<\/code><\/pre>\n<p>C++11 is even simpler, but <strong>it crashes with gcc 4.9.2 if you use the same string for source and target<\/strong>, like this:<\/p>\n<pre><code>#include <regex>\r\n std::regex reg(from);\r\n str = std::regex_replace(str,reg,to);<\/code><\/pre>\n<p>You can fix that by using a swap string:<\/p>\n<pre><code>#include <regex>\r\n regex reg(from);\r\n string newstr = regex_replace(str,reg,to);\r\n str = newstr;<\/code><\/pre>\n<p>But it isn&#8217;t\u00a0working across lines, with this particular syntax, and I&#8217;ve seen others complain that they couldn&#8217;t get multiline\u00a0syntax going. \u00a0I&#8217;ll stick with boost for now until this smooths out a little bit more. \u00a0Onwards.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I gave myself an hour to add multi-line search-and-replace support in my code. \u00a0It took 3, and C++11 std::regex_replace() gave me a hard crash with inline replacement. \u00a0The syntax can be annoying (for example, using double escape), so for reference, here&#8217;s a jumpstart. Given: using namespace std; string str = &#8220;Lots of text to search\\nacross [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[204,22,19],"tags":[190,55],"class_list":["post-1929","post","type-post","status-publish","format-standard","hentry","category-a-better-trader","category-cpp","category-opensource","tag-c11","tag-regex"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9M11L-v7","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/1929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/comments?post=1929"}],"version-history":[{"count":8,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/1929\/revisions"}],"predecessor-version":[{"id":1937,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/1929\/revisions\/1937"}],"wp:attachment":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/media?parent=1929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/categories?post=1929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/tags?post=1929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}