reg expr, extract word from string

J

joelix

Hi,

I need help with extracting some words from a string..

here is the string

alfa.20051007.beta1.20051007.aa

i want

$string1 = "alfa.20051007";
$string2 = "beta1.20051007";
$string3 = ".aa";

Thanks in advance,

Joel
 
J

John W. Krahn

I need help with extracting some words from a string..

here is the string

alfa.20051007.beta1.20051007.aa

i want

$string1 = "alfa.20051007";
$string2 = "beta1.20051007";
$string3 = ".aa";

$ perl -le'
$_ = q/alfa.20051007.beta1.20051007.aa/;
print for /\w+(?:\.\d+)?/g;
'
alfa.20051007
beta1.20051007
aa



John
 
J

Josef Moellers

Hi,

I need help with extracting some words from a string..

here is the string

alfa.20051007.beta1.20051007.aa

i want

$string1 = "alfa.20051007";
$string2 = "beta1.20051007";
$string3 = ".aa";

Well, what have YOU tried so far? Where did the code YOU wrote fail?

Depending on what you try to achieve, have a look at the split function
and bear in mind that . is a meta character in patterns.

Josef
 
A

Anno Siegel

John W. Krahn said:
$ perl -le'
$_ = q/alfa.20051007.beta1.20051007.aa/;
print for /\w+(?:\.\d+)?/g;
'
alfa.20051007
beta1.20051007
aa

That's not to specification. The final "aa" is supposed to retain the
leading dot.

I suppose the specification is just sloppy. I'm not going there.

Anno
 
J

John W. Krahn

Anno said:
That's not to specification. The final "aa" is supposed to retain the
leading dot.

I suppose the specification is just sloppy.

Yah, that's my excuse and I'm sticking with it!
I'm not going there.

Smarter than me. ;-)



John
 
G

George

Hi,

I need help with extracting some words from a string..

here is the string

alfa.20051007.beta1.20051007.aa

i want

$string1 = "alfa.20051007";
$string2 = "beta1.20051007";
$string3 = ".aa";

Thanks in advance,

Joel

use warnings;
use strict;
my $str="alfa.20051007.beta1.20051007.aa";
my ($String1,$String2,$String3)=($str=~/(.*?\d+)\.(.*\d+)(.*)/);
print"One=$String1\nTwo=$String2\nThree=$String3\n";

--
 
U

usenet

here is the string
alfa.20051007.beta1.20051007.aa

i want
$string1 = "alfa.20051007";
$string2 = "beta1.20051007";
$string3 = ".aa";

(untested, but should work every single time for every single case)

$string = "alfa.20051007.beta1.20051007.aa"

print "Take a look at this: $string\n"

print "Type the text that you want to be assigned to \$string1: ";
chomp ($string1 = <STDIN>);
print "Type the test that you want to be assigned to \$string2: ";
chomp ($string2 = <STDIN>);
print "Type the text that you want to be assigned to \$string3: ";
chomp ($string3 = <STDIN>);


(the quality of the answer should be proportional to the quality of the
question)
 

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,780
Messages
2,569,611
Members
45,286
Latest member
ChristieSo

Latest Threads

Top