$text = 'A0 peter Z0 ... A42 peter, paul and mary Z42 ... A99 mary Z99';
my @or_matches = ( $text =~ m/A(\d+)[^Z]*(peter|mary)[^Z]*Z/g );
print "@or_matches \n";
__END__
0 peter 42 mary 99 mary
####
$text = 'A0 peter Z0 ... A42 peter, paul and mary Z42 ... A99 mary Z99';
my @and_matches =( $text =~
m/
A(\d+)[^Z]*
(
(?=mary)
[^Z]*
peter
|
(?=peter)
[^Z]*
mary
)
[^Z]*Z
/xg );
print "@and_matches \n";
__END__
42 peter, paul and mary
####
print @and_matches =( $text =~
m/
A(\d+)
(
(?=
[^Z]*
mary
)
(?=
[^Z]*
peter
)
[^Z]*
)
Z\1
/xg );