468,110 Members | 1,704 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,110 developers. It's quick & easy.

regexp to list all sentences and sub sentences, with overlapping?

Hello,

Can someone please point me toward a regular expression that goes
through a string and contructs a list of sentences and part sentences,
where words are gradually dropped from the front of the current
sentence? Sound confusing?

Well perhaps an example would help? Given...

"Different countries have different ideas. Merry Christmas to all."

I'd like to output:

Different countries have different ideas.
countries have different ideas.
have different ideas.
different ideas.
Merry Christmas to all.
Christmas to all.
to all.

Is that possible?

Thanks in advance,

Tony
Jul 19 '05 #1
4 4175
Tony wrote:
Can someone please point me toward a regular expression that goes
through a string and contructs a list of sentences and part sentences,
where words are gradually dropped from the front of the current
sentence? Sound confusing?

Well perhaps an example would help? Given...

"Different countries have different ideas. Merry Christmas to all."

I'd like to output:

Different countries have different ideas.
countries have different ideas.
have different ideas.
different ideas.
Merry Christmas to all.
Christmas to all.
to all.

Is that possible?


Maybe, I don't know.
But I question if REs are the best tool for the job.

Two splits with two nested loops will do quite nicely:

use warnings; use strict;
my $s = "Different countries have different ideas. Merry Christmas to all.";
my @sentences = split /\./, $s;
for (@sentences) {
my @words = split (/ /, $_);
while (@words) {
print (join ' ',@words);
print "\n";
shift @words;
}
}

Just replace the print with a push to your result list if you want to have a
list instead.

jue
Jul 19 '05 #2
ha**********@hotmail.com (Tony) wrote in news:c90e5468.0311260046.693d35c1
@posting.google.com:
Is that possible?


Everything is possible with perl. ;)

my $s = "Different countries have different ideas. Merry Christmas to
all.";

while ($s =~ m/\s/) {
print $s."\n";
$s =~ s/[^\s]+\s(.*)/$1/;
}

Hth,

-Andy

--
Andy De Petter - http://www.techos.be/andy - na**@gunvobk.or (ROT13)
Expert IT Analyst - Belgacom ANS/NTA/NST - http://www.belgacom.be
"Cogito Ergo Sum - I think, therefore I am."
-- R. Descartes
Jul 19 '05 #3
Very impressive. Thank you very much.

But what is the second "\s" for in: $s =~ s/[^\s]+\s(.*)/$1/;

I've also decided to implement a second loop, and this time drop off
the LAST word each time. Is there a better regexp than the below
(which seems to be working):

$s =~ s/(.*)[\$\s]+.+$/$1/;

Andy De Petter <nq******@fxlarg.or> wrote in message news:<Xn******************************@195.238.3.1 80>...
ha**********@hotmail.com (Tony) wrote in news:c90e5468.0311260046.693d35c1
@posting.google.com:
Is that possible?


Everything is possible with perl. ;)

my $s = "Different countries have different ideas. Merry Christmas to
all.";

while ($s =~ m/\s/) {
print $s."\n";
$s =~ s/[^\s]+\s(.*)/$1/;
}

Jul 19 '05 #4
ha**********@hotmail.com (Tony) wrote in news:c90e5468.0311270049.41f4b553
@posting.google.com:

But what is the second "\s" for in: $s =~ s/[^\s]+\s(.*)/$1/;

To check, wheter there's still a space after a detected word.
I've also decided to implement a second loop, and this time drop off
the LAST word each time. Is there a better regexp than the below
(which seems to be working):

$s =~ s/(.*)[\$\s]+.+$/$1/;


$s =~ s/(.*)\s+[^\s]+\.?/$1/;

(or something ilke that)

-Andy
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Martin Lucas-Smith | last post: by
4 posts views Thread by Travers Naran | last post: by
9 posts views Thread by =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?= | last post: by
7 posts views Thread by cirfu | last post: by
7 posts views Thread by antar2 | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.