{"id":1715,"date":"2016-10-10T12:47:01","date_gmt":"2016-10-10T10:47:01","guid":{"rendered":"http:\/\/blog.rabahi.net\/?page_id=1715"},"modified":"2016-10-10T16:15:57","modified_gmt":"2016-10-10T14:15:57","slug":"perl-reg-exp","status":"publish","type":"page","link":"https:\/\/blog.rabahi.net\/?page_id=1715","title":{"rendered":"Perl &#8211; Regular expression"},"content":{"rendered":"<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Capture_string_elements\"><span class=\"toc_number toc_depth_1\">1<\/span> Capture string elements<\/a><\/li><li><a href=\"#Case_insensitive\"><span class=\"toc_number toc_depth_1\">2<\/span> Case insensitive<\/a><\/li><li><a href=\"#Iterate\"><span class=\"toc_number toc_depth_1\">3<\/span> Iterate<\/a><\/li><li><a href=\"#Substitution\"><span class=\"toc_number toc_depth_1\">4<\/span> Substitution<\/a><\/li><\/ul><\/div>\n<h1><span id=\"Capture_string_elements\">Capture string elements<\/span><\/h1>\n<pre lang=\"perl\">\r\nmy $string = \"hello world\";\r\n\r\nif($string=~\/^(\\w+)\\s+(\\w+)$\/) {\r\n  my firstWord = $1;\r\n  my secondWord = $1;\r\n}\r\n<\/pre>\n<p>Or you can capture quickly if you are sure that the regular expression match :<\/p>\n<pre lang=\"perl\">\r\n$string=~\/^(\\w+)\\s+(\\w+)$\/;\r\nmy firstWord = $1;\r\nmy secondWord = $1;\r\n<\/pre>\n<h1><span id=\"Case_insensitive\">Case insensitive<\/span><\/h1>\n<p>To be case insensitiv, you have to add &#8216;i&#8217; in your regular expression :<\/p>\n<pre lang=\"perl\">\r\nif($string=~\/^K(.*)$\/i) {\r\n   # word can start with 'K' or 'k'\r\n   my $result = $1;\r\n}\r\n<\/pre>\n<h1><span id=\"Iterate\">Iterate<\/span><\/h1>\n<pre lang=\"perl\">\r\nmy $string = \"hello world\";\r\n\r\nwhile($string=~\/(\\w)\/g) {\r\n  my $result = $1;\r\n}\r\n<\/pre>\n<h1><span id=\"Substitution\">Substitution<\/span><\/h1>\n<pre lang=\"perl\">\r\nmy $string = \"hello world\";\r\n\r\n$string=~s\/\\s+\/,\/g;\r\n\r\nprint \"$string\\\"n;\r\n# $> hello,world\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 Capture string elements2 Case insensitive3 Iterate4 Substitution Capture string elements my $string = &#8220;hello world&#8221;; if($string=~\/^(\\w+)\\s+(\\w+)$\/) { my firstWord = $1; my secondWord = $1; } Or you can capture quickly if you are sure that the regular expression match : $string=~\/^(\\w+)\\s+(\\w+)$\/; my firstWord = $1; my secondWord = $1; Case insensitive To be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1693,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1715","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1715"}],"version-history":[{"count":7,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1715\/revisions"}],"predecessor-version":[{"id":1746,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1715\/revisions\/1746"}],"up":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1693"}],"wp:attachment":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}