Splitting and keeping the delimiter

S

Sandman

Hello! I have a string like this:

12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak

And I want to split that in seperate "programs" that I'd like to deal with
individually. So, I want this in an array like this:

"12.00 Simpsons",
"12.30 Seindfeld"
"13.00 Movie: Dante's Peak"

And the one thing that I can split on is '\d\d\.\d\d', which is the only common
thing, but using

split /\d\d\.\d\d/

produces

"Simpsons",
"Seindfeld"
"Movie: Dante's Peak"

Is there a way? How would you do it?
 
G

Glenn Jackman

Sandman said:
Hello! I have a string like this:

12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak

And I want to split that in seperate "programs" that I'd like to deal with
individually. So, I want this in an array like this:

"12.00 Simpsons",
"12.30 Seindfeld"
"13.00 Movie: Dante's Peak"

my $str = "12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak";
my $delim = qr/\d\d\.\d\d/;
my @programs = $str =~ m{
$delim # the delimiter
.+? # followed by some characters, until ...
(?= $delim | $) # we can look ahead to see delim or the end of string
}xg;
 
J

John W. Krahn

Sandman said:
Hello! I have a string like this:

12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak

And I want to split that in seperate "programs" that I'd like to deal with
individually. So, I want this in an array like this:

"12.00 Simpsons",
"12.30 Seindfeld"
"13.00 Movie: Dante's Peak"

And the one thing that I can split on is '\d\d\.\d\d', which is the only common
thing, but using

split /\d\d\.\d\d/

produces

"Simpsons",
"Seindfeld"
"Movie: Dante's Peak"

Is there a way? How would you do it?


$ perl -le'
$string = " 12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dantes Peak ";
@array = $string =~ /\d\d\.\d\d.+?(?=\d|$)/g;
print for @array;
'
12.00 Simpsons
12.30 Seinfeld
13.00 Movie: Dantes Peak



John
 
D

David K. Wall

Sandman said:
Hello! I have a string like this:

12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak

And I want to split that in seperate "programs" that I'd like to deal with
individually. So, I want this in an array like this:

"12.00 Simpsons",
"12.30 Seindfeld"
"13.00 Movie: Dante's Peak"


You have workable solutions from several responses. Just one comment: use
split() when you know what to throw away, a regex when you know what to keep.
(I believe I'm quoting someone, but can't remember whom.)
 
U

Uri Guttman

DKW> You have workable solutions from several responses. Just one
DKW> comment: use split() when you know what to throw away, a regex
DKW> when you know what to keep. (I believe I'm quoting someone, but
DKW> can't remember whom.)

if he didn't say it first, he surely publicized the most, randal
schwartz.

uri
 
A

Anno Siegel

Uri Guttman said:
DKW> You have workable solutions from several responses. Just one
DKW> comment: use split() when you know what to throw away, a regex
DKW> when you know what to keep. (I believe I'm quoting someone, but
DKW> can't remember whom.)

if he didn't say it first, he surely publicized the most, randal
schwartz.

....so much so that the saying is sometimes called "Randal's Rule".
 
J

Janek Schleicher

Sandman wrote at Wed, 10 Sep 2003 22:29:57 +0200:
Hello! I have a string like this:

12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak

And I want to split that in seperate "programs" that I'd like to deal with
individually. So, I want this in an array like this:

"12.00 Simpsons",
"12.30 Seindfeld"
"13.00 Movie: Dante's Peak"

I wouldn't split it, as it is easier IMHO to describe what you want
instead of what you won't:

my $progstr = "12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak";
my @program = $progstr =~ /\d+\.\d+\s+.*?(?=\s+\d|$)/g;
print join "\n", @program;


Greetings,
Janek
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top