#!/usr/bin/perl
use Modern::Perl;
# 934221
# find a string, then print the next ten chars
my @content = qw/abcdABCD1234567890xyz abcd12345ABCD0ABCD ABCD1234ABC qwertyABCD1234567890/;
for my $content(@content) {
$content =~ /.+?ABCD(.{10}).*/;
say "Current array element is: $content";
if ($1) {
say "\t Next 10 char after the match: $1";
next;
}
}
####
Current array element is: abcdABCD1234567890xyz
Next 10 char after the match: 1234567890
Current array element is: abcd12345ABCD0ABCD
Current array element is: ABCD1234ABC
Current array element is: qwertyABCD1234567890
Next 10 char after the match: 1234567890
####
Current array element is: abcdABCD1234567890xyz
Next 10 char after the match: 1234567890
Current array element is: abcd12345ABCD0ABCD
Current array element is: ABCD1234ABC
Current array element is: qwertyABCD1234567890
Next 10 char after the match: 1234567890
Current array element is: ABCD 123 456 789
####
my @content = qw/abcdABCD1234567890xyz abcd12345ABCD0ABCD ABCD1234ABC qwertyABCD1234567890/;
push @content, 'ABCD 123 456 789';
say "===> \$content[4]: $content[4] \n\n";
for my $content(@content) {
$content =~ /.+?ABCD(.{10}).*/;
say "Current array element is: $content";
if ($1) {
say "\t Next 10 char after the match: $1";
}else{
say "No match on $content";
}
}
####
===> $content[4]: ABCD 123 456 789
Current array element is: abcdABCD1234567890xyz
Next 10 char after the match: 1234567890
Current array element is: abcd12345ABCD0ABCD
Next 10 char after the match: 1234567890
Current array element is: ABCD1234ABC
Next 10 char after the match: 1234567890
Current array element is: qwertyABCD1234567890
Next 10 char after the match: 1234567890
Current array element is: ABCD 123 456 789
Next 10 char after the match: 1234567890
####
for my $content(@content) {
# my $match = ''; # explicit but verbose
# my $match; # still explicit and only slightly less verbose; same effect
my ($match) = $content =~ /.+?ABCD(.{10}).*/; # less code; same effect
say "Current array element is: $content";
if ($match) {
say "\t Next 10 char after the match: $1";
}else{
say "No match on $content";
}
}
####
my @content = qw/abcdABCD1234567890xyz
abcd12345ABCD0ABCD
ABCD1234ABC
qwertyABCD12diff7890/;
# push @content, 'ABCD 123 456 789'; # See note "SOLVED!" below
push @content, 'x ABCD 123 456 789'; # afterthought addition
for my $content(@content) {
my ($match) = $content =~ /.+?ABCD(.{10}).*/; # avoid probs w/non-reset of $1
say "Current array element is: $content";
if ( $match ) {
say "\t MATCH! Next 10 char after the match: $1";
} else {
say "\t No match on $content";
}
}
=head
# SOLVED! why last array element failed to match: it initially began with 'ABCD...'
# BUT the regex required something ( '.+?' ) before ' ABCD(.{10} '
# And a better fix would be to write the regex as:
# '/.+?ABCD(.{10}).*/'
# or as: '/ABCD(.{10}).*/'
C:\>934221.pl
Current array element is: abcdABCD1234567890xyz
MATCH! Next 10 char after the match: 1234567890
Current array element is: abcd12345ABCD0ABCD
No match on abcd12345ABCD0ABCD
Current array element is: ABCD1234ABC
No match on ABCD1234ABC
Current array element is: qwertyABCD12diff7890
MATCH! Next 10 char after the match: 12diff7890
Current array element is: x ABCD 123 456 789
MATCH! Next 10 char after the match: 123 456 7
C:\>
=cut