473,466 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 4368
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Martin Lucas-Smith | last post by:
Can anyone point me to a regular expression in PHP which could be used to check that a proposed (My)SQL database/table/column name is valid, i.e. shouldn't result in an SQL error when created? ...
4
by: Travers Naran | last post by:
Here's the basic idea. I have a dictionary of substrings (the substrings stored as keys). I have a list of strings. I want to find out, for each word in the dictionary, how many times the...
3
by: Jane Doe | last post by:
Hello, I need to browse a list of hyperlinks, each followed by an author, and remove the links only for certain authors. 1. I searched the archives on Google, but didn't find how to tell the...
2
by: brian | last post by:
Hi, before coming to .NET, I utilized regular expressions mostly in JScript / JavaScript and also in my favorite text editor: TextPad (www.textpad.com) I don't know about JScript/JavaScript, but...
16
by: Brian D | last post by:
I have a multiple select list that is created dynamically based on a previous selection on an asp page. The first thing I do is to clear the curent option list by ...
9
by: =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?= | last post by:
With regexps you can search for strings matching it. For example, given the regexp: "foobar\d\d\d". "foobar123" would match. I want to do the reverse, from a regexp generate all strings that could...
7
by: cirfu | last post by:
pat = re.compile("(\w* *)*") this matches all sentences. if fed the string "are you crazy? i am" it will return "are you crazy". i want to find a in a big string a sentence containing Zlatan...
7
by: antar2 | last post by:
Hello, I can not find out how to read a file into a list of lists. I know how to split a text into a list sentences = line.split(\n) following text for example should be considered as a...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.