#!/usr/bin/perl # http://perlmonks.org/?node_id=1200434 use strict; use warnings; use re 'eval'; my $patterns = <<'END'; ^\d{2}.\d{2}.\d{2}$ date ^\d{2}.\d{2}.\d{4}$ date ^[A-Z]{2}\d{9}[A-Z]{2}$ Royal Mail Track & Trace code ^\d{16}$ visa card ^\d{13}$ EAN-13 barcode END my $regex; sub patternidentification { if( not defined $regex ) { ##################### build a single regex just once my $all = join '|', map { /^(\S+)\s++(.+)/ ? "(?:$1(?{'$2'}))" : die "bad pattern $_" } split /\n/, $patterns; $regex = qr/$all/; } return /$regex/ ? $^R : "unknown"; } ##################### then try all matches while() { chomp; my $answer = patternidentification($_); print "$_ is a $answer\n"; } __DATA__ 12 12 17 09 30 2O17 09 30 2017 09 30 12017 123123123123123 1231231231231231 12312312312312312 456456456456 4564564564567 45645645645678 QW123456789WQ