On Thu, 09 Oct 2008 20:45:59 +0200, Mario R. Sanchez, Ph.D. <[EMAIL PROTECTED]>
wrote:
> 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" 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?
>
> for example and using the above, say $n = 4
> so the results would be "cow jumps ove" (notice 4 characters to both the
> left and right of the target text $ss.
You could do that with a relatively simple regular expression.
--
#!/usr/bin/perl -w
use 5.010;
use strict;
use warnings;
my $longstring = "the cow jumps over the moon";
my $shortstring = "jumps";
if ( $longstring =~ m{(....$shortstring....)} ) {
my $result = $1;
say $1;
}
--
You may also want to review a reference for the syntax here:
http://www.greenend.org.uk/rjk/2002/06/regexp.html
A more complex solution would be:
--
#!/usr/bin/perl -w
use 5.010;
use strict;
use warnings;
my $n = 4;
my $longstring = "the cow jumps over the moon";
my $shortstring = "jumps";
my $regexp = ('.' x $n) . $shortstring . ('.' x $n);
if ( $longstring =~ m{($regexp)} ) {
my $result = $1;
say $1;
}
--
GrĂ¼sse,
Christian Walde
As too many people send me top- and fully-quoted
emails, here is a guide on how to be considerate
when sending emails to other people:
http://www.xs4all.nl/~hanb/documents/quotingguide.html
--
___________________________________________________________
Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs