reg expr, extract word from string

Discussion in 'Perl Misc' started by joelix@gmail.com, Oct 7, 2005.

  1. Guest

    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
     
    , Oct 7, 2005
    #1
    1. Advertising

  2. wrote:
    >
    > 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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Oct 7, 2005
    #2
    1. Advertising

  3. wrote:
    > 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
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Oct 7, 2005
    #3
  4. Anno Siegel Guest

    John W. Krahn <> wrote in comp.lang.perl.misc:
    > wrote:
    > >
    > > 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


    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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Oct 7, 2005
    #4
  5. Anno Siegel wrote:
    > John W. Krahn <> wrote in comp.lang.perl.misc:
    >> wrote:
    >>>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

    >
    > 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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Oct 7, 2005
    #5
  6. George Guest

    wrote:

    > 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";

    --
     
    George, Oct 7, 2005
    #6
  7. Guest

    wrote:
    > 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)
     
    , Oct 7, 2005
    #7
    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. Replies:
    1
    Views:
    337
    intrader
    Sep 26, 2005
  2. Good Night Moon
    Replies:
    9
    Views:
    285
    Rick DeNatale
    Jul 25, 2007
  3. Robin
    Replies:
    1
    Views:
    190
    Tassilo v. Parseval
    Jan 22, 2004
  4. joel
    Replies:
    4
    Views:
    147
    John W. Krahn
    Mar 18, 2005
  5. Guest
    Replies:
    4
    Views:
    296
    Guest
    May 12, 2006
Loading...

Share This Page