hi mario --
In a message dated 10/9/2008 1:46:26 P.M. Eastern Standard Time,
[EMAIL PROTECTED] writes:
> dear perl gurus
>
> can someone suggest a design/approach on solving the following
> requirement:
>
> there is a string, say $ss, that is contained in a longer string, say $ls.
> for example $ss = "jumps over" and $ls = "the cow jumps over the moon"
>
> how do you suggest obtaining a string from $ls that is $n characters
> to the both left and right of $ss?
>
> using above, say $n = 4 so results would be "cow jumps ove" (notice 4
> characters to both the left and right of the target text $ss.
>
> any approach would be much appreciated!
>
> mario sanchez
first, i assume there has been some corruption of your original e-mail
and "cow jumps ove" should read "cow jumps over the".
try something along the lines of:
perl -wMstrict -le
"my $ls = 'the cow jumps over the';
my $ss = 'jumps over';
my $n = 4;
my $regex = qr{ .{$n} \Q$ss\E .{$n} }xms;
print q{``}, $ls =~ /($regex)/, q{''};
"
``cow jumps over the''
perl -wMstrict -le
"my $ls = 'the cow jumps xxx over the';
my $ss = 'jumps over';
my $n = 4;
my $regex = qr{ .{$n} \Q$ss\E .{$n} }xms;
print q{``}, $ls =~ /($regex)/, q{''};
"
``''
perl -wMstrict -le
"my $ls = 'the cow jumps over the';
my $ss = 'jumps xxx over';
my $n = 4;
my $regex = qr{ .{$n} \Q$ss\E .{$n} }xms;
print q{``}, $ls =~ /($regex)/, q{''};
"
``''
see also the standard man pages perlre, perlretut and perlrequick
(e.g., ``perldoc perlre'').
hth -- bill walters
**************New MapQuest Local shows what's happening at your destination.
Dining, Movies, Events, News & more. Try it out
(http://local.mapquest.com/?ncid=emlcntnew00000002)
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs