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

Discussion in 'Perl' started by Tony, Nov 26, 2003.

  1. Tony

    Tony Guest

    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
    Tony, Nov 26, 2003
    #1
    1. Advertising

  2. 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
    Jürgen Exner, Nov 26, 2003
    #2
    1. Advertising

  3. (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 - (ROT13)
    Expert IT Analyst - Belgacom ANS/NTA/NST - http://www.belgacom.be
    "Cogito Ergo Sum - I think, therefore I am."
    -- R. Descartes
    Andy De Petter, Nov 26, 2003
    #3
  4. Tony

    Tony Guest

    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 <> wrote in message news:<Xns943F6BDE93314adepetteskynetbe@195.238.3.180>...
    > (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/;
    > }
    Tony, Nov 27, 2003
    #4
  5. (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
    Andy De Petter, Nov 27, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    2
    Views:
    882
  2. shortwave

    css sub-menu overlapping problem

    shortwave, Dec 16, 2007, in forum: HTML
    Replies:
    5
    Views:
    1,142
  3. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,019
    Lawrence D'Oliveiro
    May 20, 2011
  4. Joao Silva
    Replies:
    16
    Views:
    358
    7stud --
    Aug 21, 2009
  5. Ana
    Replies:
    0
    Views:
    109
Loading...

Share This Page