Perl – Using Pattern-sequence Memory with Regular Expressions

Pattern-sequence memory is very useful when you want to match the same character in several places but don’t care which character you match as long as all occurrences are the same character. For example, the below code matches the date format “dd-mm-yy” where the dashes can be any two of the same non-word characters (in regex notation the range would be [^_0-9a-zA-Z]). Pattern sequences are stored in memory from left to right, so “\1” would represent the first match.

Using pattern-sequence memory with regular expressions

Using pattern-sequence memory with regular expressions

The input file is called “dates.txt” and contains the below data. The Perl script should only print out the three lines of text in the file that meet the regular expression criteria: “12-29-11”, “12.29.11”, and “12 29 11”.

“dates.txt” file input and Perl script output

"dates.txt" input file

"dates.txt" input file

Perl script - pattern-sequence memory code output

Perl script - pattern-sequence memory code output


This entry was posted in Perl, Programming Languages, Technology and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


5 × = twenty