#!/usr/bin/perl -w use strict; sub matches_regex{ ## Purpose: Means to test wether a Regular Expression ## matches what you think it matches. ## Requires: One regex and one a scalar to use it on. ## Returns: True (1) for matches, false (0) for non-matches. ## ========================================================== ## Usage: "matches_regex( 'string to test', 'regex to use' ); ## ========================================================== my ($string, $regex) = @_; return( $string =~ /$regex/ ); } # ===== illustrations ===== my $string = '4245581234567890'; my @regexes = ('^[424558][0-9]{10,10}$' ,'^(424558)[0-9]{10,10}$' ,'^424558[0-9]{10,10}$' ); foreach my $regex( @regexes ){ print "Testing string '$string' for regex '$regex'\n"; if( matches_regex( $string, $regex ) ){ print "\tMatched\n"; } else { print "\tDid not match\n"; } }