suggestion substring and more
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?
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.
any approach would be much appreciated!
mario sanchez
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: suggestion substring and more
On Thu, 09 Oct 2008 20:45:59 +0200, Mario R. Sanchez, Ph.D. <msanc010 [at] cs.fi=
u.edu> wrote:
> can someone suggest a design/approach on solving the following requiremen=
t:
>
> there is a string, say $ss, that is contained in a longer string, say $ls.
> for example $ss =3D "jumps" and $ls =3D "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 =3D 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 =3D "the cow jumps over the moon";
my $shortstring =3D "jumps";
if ( $longstring =3D~ m{(....$shortstring....)} ) {
my $result =3D $1;
say $1;
}
--
You may also want to review a reference for the syntax here: http://www.gre=
enend.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 =3D 4;
my $longstring =3D "the cow jumps over the moon";
my $shortstring =3D "jumps";
my $regexp =3D ('.' x $n) . $shortstring . ('.' x $n);
if ( $longstring =3D~ m{($regexp)} ) {
my $result =3D $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
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: suggestion substring and more
--===============0297139443==
Content-Type: multipart/alternative;
boundary="-----------------------------1223580187"
-------------------------------1223580187
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit
hi mario --
In a message dated 10/9/2008 1:46:26 P.M. Eastern Standard Time,
msanc010 [at] cs.fiu.edu 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)
-------------------------------1223580187
Content-Type: text/html; charset="US-ASCII"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3DUS-ASCII">
<META content=3D"MSHTML 6.00.2800.1479" name=3DGENERATOR></HEAD>
<BODY id=3Drole_body style=3D"FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: =
Arial"
bottomMargin=3D7 leftMargin=3D7 topMargin=3D7 rightMargin=3D7><FONT id=3Drol=
e_document
face=3DArial color=3D#000000 size=3D2>
<DIV>
<DIV>hi mario -- </DIV>
<DIV> </DIV>
<DIV>In a message dated 10/9/2008 1:46:26 P.M. Eastern Standard Time,
msanc010 [at] cs.fiu.edu writes:</DIV>
<DIV> </DIV>
<DIV>> dear perl gurus<BR>> <BR>> can someone suggest a design/appr=
oach
on solving the following<BR>> requirement:<BR>> <BR>> there is a
string, say $ss, that is contained in a longer string, say $ls.<BR>> for
example $ss =3D "jumps over" and $ls =3D "the cow jumps over the moon"<BR>&g=
t;
<BR>> how do you suggest obtaining a string from $ls that is $n
characters<BR>> to the both left and right of $ss?<BR>> <BR>> using=
above, say $n =3D 4 so results would be "cow jumps ove" (notice 4<BR>>
characters to both the left and right of the target text $ss.<BR>> <BR>&g=
t;
any approach would be much appreciated!<BR>> <BR>> mario sanchez<BR></=
DIV>
<DIV> </DIV>
<DIV>first, i assume there has been some corruption of your original e-mail
</DIV>
<DIV>and "cow jumps ove" should read "cow jumps over the". </DIV=
>
<DIV> </DIV>
<DIV>try something along the lines of: </DIV>
<DIV> </DIV>
<DIV>perl -wMstrict -le<BR>"my $ls =3D 'the cow jumps over the';<BR> my=
$ss =3D
'jumps over';<BR> my $n =3D 4;<BR> my $regex =3D qr{ .{$n} \=
Q$ss\E
..{$n} }xms;<BR> print q{``}, $ls =3D~ /($regex)/, q{''};<BR>"<BR>``cow =
jumps
over the''</DIV>
<DIV> </DIV>
<DIV>perl -wMstrict -le<BR>"my $ls =3D 'the cow jumps xxx over the';<BR>&nbs=
p;my
$ss =3D 'jumps over';<BR> my $n =3D 4;<BR> my $regex =3D qr{=
.{$n}
\Q$ss\E .{$n} }xms;<BR> print q{``}, $ls =3D~ /($regex)/,
q{''};<BR>"<BR>``''</DIV>
<DIV> </DIV>
<DIV>perl -wMstrict -le<BR>"my $ls =3D 'the cow jumps over the';<BR> my=
$ss =3D
'jumps xxx over';<BR> my $n =3D 4;<BR> my $regex =3D qr{ .{$=
n}
\Q$ss\E .{$n} }xms;<BR> print q{``}, $ls =3D~ /($regex)/,
q{''};<BR>"<BR>``''</DIV>
<DIV> </DIV>
<DIV>see also the standard man pages perlre, perlretut and perlrequick </DIV=
>
<DIV>(e.g., ``perldoc perlre''). </DIV>
<DIV> </DIV>
<DIV>hth -- bill walters </DIV>
<DIV> </DIV></DIV></FONT><BR><BR><BR><DIV CLASS=3D"aol_ad_footer" ID=
=3D"5e6259ca8de2b77e267dbe98ee2ffd"><FONT style=3D"color: black; font: norma=
l 10pt ARIAL, SAN-SERIF;"><HR style=3D"MARGIN-TOP: 10px">New <b>MapQuest Loc=
al</b> shows what's happening at your destination. Dining, Movies, Events, N=
ews & more. <a href=3D"http://local.mapquest.com/?ncid=3Demlcntnew00000002">=
Try it out! </a></FONT></DIV></BODY></HTML>
-------------------------------1223580187--
--===============0297139443==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0297139443==--
RE: suggestion substring and more
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?
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.
---------------------
use strict;
my $ss = "jumps";
my $ls = "the cow jumps over the moon";
my $n = 4;
$ls =~ /$ss/;
print "'". substr($`,0-$n) . $& . substr($', 0, $n) . "'";
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: suggestion substring and more
On Thu, 09 Oct 2008 21:29:19 +0200, Bullock, Howard A. <HBullock [at] tycoelectr=
onics.com> wrote:
> print "'". substr($`,0-$n) . $& . substr($', 0, $n) . "'";
You should really
use English;
if you're gonna do it that way. ;)
http://perldoc.perl.org/perlvar.html
-- =
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
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: suggestion substring and more
Mario R. Sanchez, Ph.D. <> wrote:
> 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?
>
> 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.
Surely, that should be "cow jumps over the", i.e. including the 4
characters either side of "jumps over".
>
> any approach would be much appreciated!
Well, its easy to find out the offset of the substring (see 'perldoc -f
index'), and you know how long the substring is (see 'perldoc -f
length'), so its just some simple arithmetic and a call to substr (see
'perldoc -f substr') to extract what you want.
HTH
--
Brian Raven
------------------------------------------------------------ -----------------------------------------------
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs