{"id":36152,"date":"2018-11-13T21:19:43","date_gmt":"2018-11-13T21:19:43","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=36152"},"modified":"2018-11-13T21:23:33","modified_gmt":"2018-11-13T21:23:33","slug":"computer-science-perl-regex-grouping-with-parentheses","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/computer-science-perl-regex-grouping-with-parentheses\/","title":{"rendered":"Computer science: Perl regex: grouping with parentheses"},"content":{"rendered":"<h2>Self-tutoring about computer science: the tutor mentions using parentheses in Perl regular expressions to form groups.<\/h2>\n<p>A useful feature of regular expressions is the optional use of parentheses. There are numerous reasons to use them, but one is to report parts of the match.<\/p>\n<p>Consider the following:<\/p>\n<div style=\"background-color:brown;color:white;font-family:monospace;font-size:120%;white-space:pre-wrap;margin-bottom:16px;padding-left:8px\">\n$strng=&#8221;Hehe_12345_68d-910&#8243;;<\/p>\n<p>if($strng=~m\/(hehe(_[\\d]+_)([\\d]+d)(-[d]+))\/i){<\/p>\n<p>print &#8220;$1\\n$2\\n$3\\n$4\\n&#8221;;<\/p>\n<p>}<br \/>\nelse{<\/p>\n<p>print &#8220;no match&#8221;;<br \/>\n}<\/p>\n<\/div>\n<p>The output would be (assuming no typos:)<\/p>\n<div style=\"font-family:monospace;font-size:120%;margin-bottom:16px\">\nHehe_12345_68d-910<br \/>\n_12345_<br \/>\n68d<br \/>\n-910\n<\/div>\n<p>$1 denotes the contents of the first set of parentheses in the matching pattern, $2 the second, and so on. The groupings can be used to organize the output from the match.<\/p>\n<p>Source:<\/p>\n<p><a href=\"https:\/\/perldoc.perl.org\/perlretut.html\">perldoc.perl.org<\/a><\/p>\n<p><a href=\"http:\/\/www.sthomas.net\/roberts-perl-tutorial.htm\">roberts perl tutorial<\/a><\/p>\nJack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.\n","protected":false},"excerpt":{"rendered":"<p>Self-tutoring about computer science: the tutor mentions using parentheses in Perl regular expressions to form groups. A useful feature of regular expressions is the optional use of parentheses. There are numerous reasons to use them, but one is to report &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/computer-science-perl-regex-grouping-with-parentheses\/\"> <span class=\"screen-reader-text\">Computer science: Perl regex: grouping with parentheses<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2943],"tags":[],"class_list":["post-36152","post","type-post","status-publish","format-standard","hentry","category-perl"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/36152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=36152"}],"version-history":[{"count":6,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/36152\/revisions"}],"predecessor-version":[{"id":36158,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/36152\/revisions\/36158"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=36152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=36152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=36152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}