{"id":13150,"date":"2015-11-18T19:24:18","date_gmt":"2015-11-18T19:24:18","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=13150"},"modified":"2015-11-18T19:24:18","modified_gmt":"2015-11-18T19:24:18","slug":"perl-a-password-sort-of-using-regular-expression-regex-power","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/perl-a-password-sort-of-using-regular-expression-regex-power\/","title":{"rendered":"Perl:  a password (sort of) using regular expression (regex) power"},"content":{"rendered":"<h1>The tutor shows off some of Perl&#8217;s pattern matching (aka regex) talents.<\/h1>\n<p>Let&#8217;s imagine you have a laptop you share with a friend for project work.  They&#8217;re not million-dollar secrets you&#8217;re keeping, but on the other hand, you don&#8217;t want just anyone reading those files.  At the same time, your friend is very forgetful, likely not to remember a typical password.<\/p>\n<p>To accommodate your friend, while keeping out the casual snoops, you settle on a three-character password with a variable middle character.  Let&#8217;s imagine the password is <span style=\"font-family:monospace\">n[any character]n<\/span>.  Then, <span style=\"font-family:monospace\">nan<\/span> or <span style=\"font-family:monospace\">n@n<\/span> will succeed, but <span style=\"font-family:monospace\">nano<\/span> won&#8217;t (too long).  Of course, the first and third characters must both be n.<\/p>\n<p>Pattern matching is done using regular expressions, or regex.<\/p>\n<p>Here is the Perl code that can accept the entry, then decide if it matches:<br \/>\n<span style=\"font-family:monospace;color:brown\"><br \/>\nprint &#8220;Enter your password, please.\\n\\n&#8221;;<br \/>\n<br \/>\n$word=&lt;STDIN&gt;;<br \/>\n<br \/>\nif ($word=~\/^n.n$\/){<span style=\"color:orange\">&nbsp;&nbsp;#the regex line<\/span><br \/>\nprint &#8220;Correct.\\n&#8221;;<br \/>\n}<br \/>\n<br \/>\nelse{<br \/>\nprint &#8220;Incorrect password; access denied.\\n&#8221;;<br \/>\n}<\/span><\/p>\n<p>I&#8217;ll be explaining some nuts and bolts of the regex line in a coming post:)<\/p>\n<p>Source:<\/p>\n<p>McGrath, Mike.  <u>Perl in easy steps<\/u>.  Southam:  Computer Step, 2004.<\/p>\n<p><a href=\"http:\/\/www.sthomas.net\/roberts-perl-tutorial.htm\">Robert&#8217;s Perl tutorial<\/a><\/p>\n<p>Jack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The tutor shows off some of Perl&#8217;s pattern matching (aka regex) talents. Let&#8217;s imagine you have a laptop you share with a friend for project work. They&#8217;re not million-dollar secrets you&#8217;re keeping, but on the other hand, you don&#8217;t want &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/perl-a-password-sort-of-using-regular-expression-regex-power\/\"> <span class=\"screen-reader-text\">Perl:  a password (sort of) using regular expression (regex) power<\/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":[105],"tags":[1201,173,710,709],"class_list":["post-13150","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-pattern-matching","tag-perl-programming","tag-regex","tag-regular-expressions"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/13150","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=13150"}],"version-history":[{"count":22,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/13150\/revisions"}],"predecessor-version":[{"id":13172,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/13150\/revisions\/13172"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=13150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=13150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=13150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}