use strict;
my $str = '======== local phone
765-43-21
7654321
765-4321
======== operator phone
(921) 765-43-21
(921) 7654321
(921) 765-4321
(921)765-43-21
(921)7654321
(921)765-4321
921765-43-21
9217654321
921765-4321
921-765-43-21
921-7654321
921-765-4321
======== world phone
+7 (921) 765-43-21
+7(921) 765-43-21
+7-(921) 765-43-21
7 (921) 765-43-21
7(921) 765-43-21
8 (921) 765-43-21
8(921) 765-43-21
8-(921) 765-43-21
8 (921) 765-43-21
8(921) 765-43-21
8-(921) 765-43-21
+7 9217654321
+79217654321
+7-9217654321
7 9217654321
79217654321
7-9217654321
8 9217654321
89217654321
8-9217654321
8 9217654321
89217654321
8-9217654321
======== not found
>>>>>>>>> 1. Коржев Артём Борисович 8-921-641-82-15;
======== bad
0 0 0 0
333 000
1 1 1205 1320 321
======== разбор
# world phone
(
(8|\\+?7)
[\\- ]?
)?
# operator phone
(
\\(?
\\d{3}
\\)?
[\\- ]?
)?
# local phone
[\\d\\- ]{7,10}';
my $regex = qr/((8|\+7?)[\- ]?)?(\(?\d{3}\)?[\- ]?)?([\d\- ]{7,10})/p;
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