use strict;
my $str = '08142 655456-712
0049 8142 655456-712
0049 (8142) 655456
+49 (8142) 655456
+49(8142)655456
+49 8142 655456
(+351) 282 43 50 50
90191919908
555-8909
001 6867684
001 6867684x1
1 (234) 567-8901
1-234-567-8901 x1234
1-234-567-8901 ext1234
1-234 567.89/01 ext.1234
1(234)5678901x1234
(123)8575973
(0055)(123)8575973
';
my $regex = qr/^(
(?:\(?
(?:00|\+)
(?:[1-4]\d\d|[1-9]\d?)
\)?)?
[\-\.\ \\\/]?
)?
(
(?:\(?\d{1,}\)?
[\-\.\ \\\/]?){0,}
)
(?:
[\-\.\ \\\/]?
(?:
#|
ext\.?|
extension|
x
)
[\-\.\ \\\/]?
(\d+)
)?$/mixp;
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