use strict; use warnings; use DBI; my $dbfile = 'D:\Delme~~\demo.db'; unlink $dbfile; my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","",""); $dbh->do (qq{CREATE TABLE Demo (rowid INTEGER, old VARCHAR(9), new VARCHAR(3))}); my @testData = ( ['ABFD-1234' => 'AAA'], ['ABFD-178G' => 'BBB'], ['F2HB-9401' => 'AAA'], ['ZDDR-00W5' => 'DDD'], ['D7*D-48*6' => 'EEE'], ['*7RD-4896' => 'FFF'], ['D7RD-489*' => 'GGG'], ); my @old = map {$_->[0]} @testData; my @new = map {$_->[1]} @testData; my $sth = $dbh->prepare (qq{INSERT INTO Demo (old, new) VALUES (?, ?)}); my $entries = $sth->execute_array ({}, \@old, \@new); $sth = $dbh->prepare (qq{SELECT old, new FROM Demo WHERE old LIKE ?}); $sth->execute ('%*%'); my @candidates = @{$sth->fetchall_arrayref ()}; for my $target ('D7PD', 'D7RD', '4897', '4876') { for my $candidate (map {$_->[0]} @candidates) { my @parts = split '\*', $candidate; my @rParts = reverse map {scalar reverse $_} @parts; my $str = $target; my $rStr = reverse $target; eval { matchPart($str, $_) || return for @parts; return !length $str; } or eval { $str = ''; rMatchPart($rStr, $_) || return for @rParts; return ! length $rStr; } or next; print "Matched '$target' against '$candidate'\n" if !length $str; } } sub matchPart { my ($str, $match) = @_; $match = substr $match, 0, length $_[0]; return 1 if $_[0] =~ s/$match.?//; } sub rMatchPart { my ($str, $match) = @_; $match = substr $match, 0, length($str) - 1 if length $str; return 1 if $_[0] =~ s/.?$match//; } #### Matched 'D7PD' against 'D7*D-48*6' Matched 'D7RD' against 'D7*D-48*6' Matched 'D7RD' against '*7RD-4896' Matched 'D7RD' against 'D7RD-489*' Matched '4897' against 'D7RD-489*'