use strict;
my $str = '# Standard
01386 444400
01386444400
+441386444400
+44 1386 444400
+44 1386444400
# 5 Digit
01386 41204
0138641204
+44138641204
+44 1386 41204
+44 138641204
# 3 Digit area codes (e.g. London)
+442074343046
02074343046
020 74343046
020 7434 3046
# 4 Digit area codes (e.g. Leeds)
+441133971337
01133971337
0113 3971337
# Commercial
08451772266
0845 1772266
# Mobile number (should not match)
07999888777
+447999888777
07999 888 777
+44 7999888777
';
my $regex = qr/^(?:\+44\s?|0)[1238]\d\s?(?:\d\s?){7,8}$/mp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html