^(\d{9}):\s.*((?:H\d{9})?)
1 2 3 4 5 6 7 8 9 10 11 12 13
use strict; my $re = qr/^(\d{9}):(?:.*?(H\d{9})|)/; while(<DATA>) { if (/$re/) { print "\$1 = $1; \$2 = $2\n"; } } __DATA__ 410304100: 410304100 410304101 410304100: H410304100 410304101 410304100: 410304100 H410304101
Quote$1 = 410304100; $2 =
$1 = 410304100; $2 = H410304100
$1 = 410304100; $2 = H410304101
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#! /usr/bin/perl use strict; use warnings; use 5.010.000; my $re = qr{ ^(\d{9}) # save in $1 :\s # fixed string (?: # group without saving .*? # any string, as short as possible (H\d{9}) # save in $2 )? # the "not-saving" group is optional (for lines without matching the pattern for $2) }x; while ( my $line = <DATA> ) { if ( $line =~ $re ) { # print line numbers and matches printf "%d : \$1 = %s; \$2 = %s\n", $., ( $1 // '' ), ( $2 // '' ); } } __DATA__ 410304100: 410304100 410304101 410304100: H410304100 410304101 410304100: 410304100 H410304101
1
2
3
1 : $1 = 410304100; $2 =
2 : $1 = 410304100; $2 = H410304100
3 : $1 = 410304100; $2 = H410304101