#!/usr/bin/perl use warnings; use strict; print match("one two three","four five six"); print match("one two three","four onefive six"); print match("one two three","three five six"); sub match { my @left = split /\s+/, $_[0]; my @right = split /\s+/, $_[1]; return ( join( ',', ',', @left, ',' ) =~ /,(${\join'|',map quotemeta $_, @right}),/ ) ? 1 : 0; }