#! /usr/local/bin/perl -w #LongestMatch.pl #from Friedl: Mastering Regular Expressions, pp. 334-335 use strict; my $longest_match = undef; # We'll keep track of the longest match here my $RecordPossibleMatch = qr{ (?{ # Check to see if the current match ($&) is the longest so far if (not defined($longest_match) or length($&) > length($longest_match)) { $longest_match = $&; } }) (?!) # Force failure so we'll backtrack to find further "matches" }x; while (<>) { chomp; $_ =~ m{ [abcdef]{0,1} [gh]{0,1} [ij]{0,1} [klmn]{0,1} [op]{0,1} [qr]{0,1} [astuv]{0,1} [gh]{0,1} [wxyz]{0,1} $RecordPossibleMatch }x; # Now report the accumulated result, if any if (defined($longest_match)) { print "$longest_match\n"; } else { print ".\n"; } $longest_match = undef; } #### #! /usr/local/bin/perl -w #Align.pl use strict; my %Lookup = (a => 0, b => 0, c => 0, d => 0, e => 0, f => 0, g => 1, h => 1, i => 2, j => 2, k => 3, l => 3, m => 3, n => 3, o => 4, p => 4, q => 5, r => 5, s => 6, t => 6, u => 6, v => 6, w => 8, x => 8, y => 8, z => 8); while (<>) { chomp; my @letters = split(//); my $final_string = '.' x 10; my $count = 0; foreach my $letter (@letters) { if (exists $Lookup{$letter}) { substr ($final_string, $Lookup{$letter}, 1, $letter); $count++; if ($count = 1) { $Lookup{"a"} = 6; #Allow for a later occurrence } if ($count = 2) { $Lookup{"g"} = 7; $Lookup{"h"} = 7; } } } print "$final_string\n"; $Lookup{"a"} = 0; #Reset for next word $Lookup{"g"} = 1; $Lookup{"h"} = 1; }